Dx=@(t) Dr(t).*cos(t)-r(t).*sin(t);
Dy=@(t) Dr(t).*sin(t)+r(t).*cos(t);
g=@(t) P(x(t),y(t)).*Dx(t)+Q(x(t),y(t)).*Dy(t);
circulacio=integral(g,0,pi/3);
fprintf('circulacio = %.6f\n',circulacio)
P=@(x,y) 2.50*x.^3-2.97*y;
y=@(t) t.^3.*sin(3.80*t);
Dx=@(t) -2*sin(exp(t.^2)).*exp(t.^2).*t;
Dy=@(t) 3*t.^2.*sin(3.80*t)+3.80*t.^3.*cos(3.80*t);
g=@(t) P(x(t),y(t)).*Dx(t)+Q(x(t),y(t)).*Dy(t);
circulacio=integral(g,0,pi);
fprintf('circulacio = %.6f\n',circulacio)
x=@(t) exp(1.20*t).*cos(t);
y=@(t) exp(-1.20*t).*sin(t);
Dx=@(t) exp(1.20*t).*(1.20*cos(t)-sin(t));
Dy=@(t) exp(-1.20*t).*(cos(t)-1.20*sin(t));
g=@(t) P(x(t),y(t),z(t)).*Dx(t)+Q(x(t),y(t),z(t)).*Dy(t)...
+R(x(t),y(t),z(t)).*Dz(t);
circul=integral(g,0,2*pi);
fprintf('circulacio = %.6e\n',circul)
circulacio = -1.279589e+05
r=@(t) 2.83*sin(3*t)+0.05*sin(30*t).^2;
Dr=@(t) 2.83*3*cos(3*t)+0.05*60*sin(30*t).*cos(30*t);
Dx=@(t) Dr(t).*cos(t)-r(t).*sin(t);
Dy=@(t) Dr(t).*sin(t)+r(t).*cos(t);
nt=@(t) sqrt(Dx(t).^2+Dy(t).^2);
x0=intx/long; %coordenada x del centre
h2=@(t) (x(t)-x0).^2.*nt(t); % |x(t)-x0| es la distancia a la recta x=x0
I=rho*integral(h2,t0,tf);
x=@(t) a*cos(t); y=@(t) b*sin(t);
Dx=@(t) -a*sin(t); Dy=@(t) b*cos(t);
g=@(t) P(x(t),y(t)).*Dx(t)+Q(x(t),y(t)).*Dy(t);
circulacio=integral(g,0,2*pi);
fprintf('circulacio = %.6f\n',circulacio)
f=@(x,y) 1./(a*x.^2+y.^4);
x=@(t) b*cos(t); y=@(t) sin(t);
Dx=@(t) -b*sin(t); Dy=@(t) cos(t);
N=1000; h=pi/N; tv=0:h:pi;
normav=sqrt(Dxv.^2+Dyv.^2);
longaprox=trapz(tv,normav);
mitjanaaprox=intaprox/longaprox;
fprintf('mitjanaaprox = %.6f\n',mitjanaaprox)
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
nnpolars=@(r,th) nn(r.*cos(th),r.*sin(th)).*r;
massa=integral2(nnpolars,0,1,0,2*pi);
T=@(x,y,z) 2.10*x.^2+1.70*y.^2+3.10*z;
g=@(x,y) T(x,y,z(x,y)).*nn(x,y);
gpolars=@(r,th) g(r.*cos(th),r.*sin(th)).*r;
intg=integral2(gpolars,0,1,0,2*pi);
fprintf('Tm = %.6f\n',Tm)
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
rho=@(x,y,z) (x-1).^2 + y.*z;
g=@(x,y) rho(x,y,z(x,y)).*nn(x,y);
gpolars=@(r,th) g(r.*cos(th),r.*sin(th)).*r;
rmin=sqrt(3.40-0.60); rmax=sqrt(3.40);
%corona circular de radis rmin i rmax
massa=integral2(gpolars,rmin,rmax,0,2*pi);
fprintf('massa = %.6f\n',massa)
Dhx=@(x,y) 3.*x.^2; Dhy=@(x,y) -3.*y.^2;
nn=@(x,y) sqrt(1+Dhx(x,y).^2+Dhy(x,y).^2);
g=@(x,y) rho(x,y,h(x,y)).*nn(x,y); %funcio a integrar
gpolars=@(r,th) g(r.*cos(th),r.*sin(th)).*r;
massa=integral2(gpolars,0,sqrt(1.44),0,2*pi);
fprintf('massa = %.6f\n',massa)
z=@(x,y) 1+1.90*x.^4+2.80*y.^2;
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
area=integral2(nn,-1,1,-1,1);
g=@(x,y) z(x,y).*nn(x,y);
I=integral2(g,-1,1,-1,1);
fprintf('zCG = %.6f\n',zc)
z=@(x,y) x.^2/a2+y.^2/b2;
Dzx=@(x,y) 2*x/a2; Dzy=@(x,y) 2*y/b2;
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
g=@(x,y) rho(x,y,z(x,y)).*nn(x,y);
a=sqrt(a2); b=sqrt(b2); %semieixos ellipse
gpolars=@(r,th) g(a*r.*cos(th),b*r.*sin(th)).*(a*b*r);
%hem passat a polars adaptades i multiplicat pel jacobia
massa=integral2(gpolars,0,1,0,2*pi);
fprintf('massa = %.6f\n',massa)
Q=@(x,y,z) 1.60*x.^4.*exp(x.^2);
% aplicant el teorema de Gauss:
% tanquem S per un tros de pla (ellipse inclinada)
% que es projecta sobre el cercle de centre (b/2,0) i radi b/2;
% com que div(F)=0 nomes hem de calcular el flux sobre el tros de pla
h=@(x,y) b-x; %el pla vist com a grafica
g1=@(x,y) -P(x,y,h(x,y))+R(x,y,h(x,y));
%producte escalar de F amb el vector normal del pla (-1,0,1)
g1polars=@(r,th) g1(b/2+r.*cos(th),r.*sin(th)).*r;
flux1=integral2(g1polars,0,b/2,0,2*pi);
fprintf('flux1 = %.6f\n',flux1)
% parametritzant el tros d'ellipsoide com a grafica,
% amb domini el mateix cercle de centre (b/2,0) i radi b/2
z=@(x,y) sqrt(b^2-x.^2+2*y.^2);
g2=@(x,y) -P(x,y,z(x,y)).*Dzx(x,y)-Q(x,y,z(x,y)).*Dzy(x,y)+R(x,y,z(x,y));
g2polars=@(r,th) g2(b/2+r.*cos(th),r.*sin(th)).*r;
flux2=integral2(g2polars,0,b/2,0,2*pi);
fprintf('flux2 = %.6f\n',flux2)
P=@(x,y,z) x.*z.*cos(beta*y.^2);
Q=@(x,y,z) exp(beta*y.*z);
R=@(x,y,z) y.^2.*sin(beta*x.^2);
%parametritzant amb esferiques
x=@(th,phi) A*cos(phi).*cos(th);
y=@(th,phi) A*cos(phi).*sin(th);
nx=@(th,phi) A*cos(phi).*x(th,phi);
ny=@(th,phi) A*cos(phi).*y(th,phi);
nz=@(th,phi) A*cos(phi).*z(th,phi);
%el vector normal es (nx,ny,xz)=A*cos(phi)*(x,y,z)
P(x(th,phi),y(th,phi),z(th,phi)).*nx(th,phi)...
+Q(x(th,phi),y(th,phi),z(th,phi)).*ny(th,phi)...
+R(x(th,phi),y(th,phi),z(th,phi)).*nz(th,phi);
flux1=integral2(g1,0,2*pi,pi/4,pi/2);
fprintf('flux1 = %.6f\n',flux1)
%parametritzant com a grafica
zgr=@(x,y) sqrt(A^2-x.^2-y.^2);
-P(x,y,zgr(x,y)).*Dzx(x,y)-Q(x,y,zgr(x,y)).*Dzy(x,y)+R(x,y,zgr(x,y));
g2polars=@(r,th) g2(r.*cos(th),r.*sin(th)).*r;
flux2=integral2(g2polars,0,A/sqrt(2),0,2*pi);
fprintf('flux2 = %.6f\n',flux2)
xc=@(z) sqrt(1+2.10*z.^2); %corba generatriu (tros d'hiperbola)
xs=@(th,z) xc(z).*cos(th); %sup. de revolucio (hiperboloide 1 fulla)
ys=@(th,z) xc(z).*sin(th);
nn=@(z) xc(z).*sqrt(Dxc(z).^2+1);
area=pi*integral(nn,-5.70,5.70); %meitat sup. de revolucio
ydS=@(th,z) ys(th,z).*nn(z);
inty=integral2(ydS,0,pi,-5.70,5.70);
fprintf('yCG = %.6f\n',yCG)
Dzx=@(x,y) 2*x; Dzy=@(x,y) 2*y;
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
g=@(x,y) rho(x,y,z(x,y)).*nn(x,y);
gpolars=@(r,th) g(r.*cos(th),r.*sin(th)).*r;
carrega=integral2(gpolars,0,sqrt(7.70-4.20),0,2*pi);
fprintf('carrega = %.6f\n',carrega)
Dz=@(t) 3.60./sqrt(1+7.20*t);
elem=@(t) sqrt(1+Dz(t).^2).*r(t);
Area=2*pi*integral(elem,0,2.60);
zelem=@(t) z(t).*elem(t);
zc=2*pi*integral(zelem,0,2.60)/Area;
fprintf('zCG = %.6f\n',zc)
z=@(x,y) 4.10*x.^2-1.50*y.^2;
g=@(x,y) -P(x,y,z(x,y)).*Dzx(x,y)-Q(x,y,z(x,y)).*Dzy(x,y)+R(x,y,z(x,y));
flux=integral2(g,0,1,ymin,ymax);
fprintf('flux = %.6f\n',flux)
h=@(x,y) 1.20*x.^2+1.50*y.^2;
nn=@(x,y) sqrt(1+Dhx(x,y).^2+Dhy(x,y).^2);
g=@(x,y) rho(x,y,h(x,y)).*nn(x,y);
ymax=@(x) (1-0.90*x)/0.60; %funcio
massa=integral2(g,0,x2,0,ymax);
fprintf('massa = %.6f\n',massa)
h=@(x,y) sqrt(1-x.^2/a^2-y.^2/b^2);
Dhx=@(x,y) (-x/a^2)./h(x,y);
Dhy=@(x,y) (-y/b^2)./h(x,y);
g=@(x,y) -P(x,y,h(x,y)).*Dhx(x,y)-Q(x,y,h(x,y)).*Dhy(x,y)+R(x,y,h(x,y));
% fent la integral doble iterada:
ymin=@(x) -b*sqrt(1-x.^2/a^2);
ymax=@(x) b*sqrt(1-x.^2/a^2);
flux1=integral2(g,-a,a,ymin,ymax);
fprintf('flux1 = %.6f\n',flux1)
% fent la integral doble per polars adaptades:
gpolars=@(r,th) g(a*r.*cos(th),b*r.*sin(th)).*(a*b*r);
flux2=integral2(gpolars,0,1,0,2*pi);
fprintf('flux2 = %.6f\n',flux2)
% usant el teorema de Gauss, i fent la integral triple
% per esferiques adaptades:
divF=@(x,y,z) 2*x.*cos(x.^2.*y)+2;
divF(a*r.*cos(phi).*cos(th),b*r.*cos(phi).*sin(th),r.*sin(phi))...
flux3=integral3(divFesf,0,1,0,2*pi,0,pi/2);
fprintf('flux3 = %.6f\n',flux3)
arrel=@(x,y) sqrt(1-x.^2/4-y.^2/9);
Dzx=@(x,y) -1.10*x./(4*arrel(x,y));
Dzy=@(x,y) -1.10*y./(9*arrel(x,y));
nn=@(x,y) sqrt(1+Dzx(x,y).^2+Dzy(x,y).^2);
area=integral2(nn,0,1,ymin,ymax);
fprintf('area = %.6f\n',area)
R=@(t,x,y,z,w) y-1.70*z+exp(t);
S=@(t,x,y,z,w) -x+2*w+1.40*sin(t);
F=@(t,X) [P(t,X(1),X(2),X(3),X(4));Q(t,X(1),X(2),X(3),X(4));...
R(t,X(1),X(2),X(3),X(4));S(t,X(1),X(2),X(3),X(4))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('diferencia = %.6f\n',dif)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
%usant ode45 dos cops, amb els dos temps finals tf1 i tf2:
[t,X1]=ode45(F,[t0,tf1],X0,opcions);
[t,X2]=ode45(F,[t0,tf2],X0,opcions);
dif1=h(X2(end,:))-h(X1(end,:));
fprintf('diferencia1 = %.6f\n',dif1)
%usant ode45 nomes un cop, especificant tf1 i tf2:
[t,X3]=ode45(F,[t0,tf1,tf2],X0,opcions);
%la matriu X3 nomes te 3 files, que corresponen als temps t0,tf1,tf2
dif2=h(X3(3,:))-h(X3(2,:));
fprintf('diferencia2 = %.6f\n',dif2)
R=@(t,x,y,z) 2*z-4*y-5*x+cos(t);
F=@(t,X) [X(2);X(3);R(t,X(1),X(2),X(3))];
opcions = odeset('AbsTol',1e-8,'RelTol',1e-8);
t0=0; X0=[2.80,1.50,2.40];
[t,X]=ode45(F,[t0,tf],X0,opcions);
suma=X(end,1)+X(end,2)-X(end,3);
fprintf('suma = %.6f\n',suma)
Q=@(t,x,y) -x+2*x.^2-1.30*t.*y;
F=@(t,X) [P(X(1),X(2));Q(t,X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('x(1) = %.6f\n',xf)
P=@(t,x,y) 0.14*t.*(x+y);
F=@(t,X) [P(t,X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('x(1) = %.6f\n',xf)
F=@(t,X) [X(2);Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,t1],X0,opcions);
[t,X]=ode45(F,[t0,t2],X0,opcions);
fprintf('distancia = %.6f\n',dist)
Q=@(x,y) 1.04*x.^3-1.52*y;
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('suma = %.6f\n',suma)
P=@(x,y) 2*x-x.*y+a*x.*(5-x);
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('solucio = %.6f\n',solucio)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
fprintf('xf = %.6f\n',xf)
F=@(t,X) [X(2);Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
[t,X]=ode45(F,[t0,tf],X0,opcions);
E=@(x,y) y.^2/2+a*(1-cos(x));
energia=E(X(end,1),X(end,2));
fprintf('energia = %.6f\n',energia)
F=@(t,X) [X(2);Q(X(1),X(2))];
E=@(x,y) y.^2/2+g*(1-cos(x));
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events', @(t,X) condicioE06(t,X,E,E0));
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('temps = %.6f\n',te)
P=@(x,y) 3*x-2*y+0.30*sin(x.*y);
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE07);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
xtall=Xe(jtall(2),1); %notem que jtall(1) correspon a la condicio inicial
fprintf('xtall = %.6f\n',xtall)
F=@(t,X) [X(2);Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE08);
y0=sqrt(2*E0); %aillant y=+sqrt(...) de E(0,x')=E0 i avaluant en x=0
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
temps=te(3); %volem el segon tall
fprintf('periode = %.6f\n',temps)
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events', @condicioE09);
% definim una primera EDO amb f(t)=0 i la resolem fins a l'instant T0,
% i prenem el punt final X1(end,:) obtingut, com a condicio inicial
% d'una segona EDO amb f(t)=t-T0
F1=@(t,X) [X(2);Q1(X(1),X(2))];
[t1,X1]=ode45(F1,[0,T0],[0,0.70],opcions);
Q2=@(t,x,y) -8*x-0.19*y+f(t);
F2=@(t,X) [X(2);Q2(t,X(1),X(2))];
[t2,X2,te,Xe]=ode45(F2,[T0,tf],X1(end,:),opcions);
fprintf('te = %.6f\n',te)
% definim una unica EDO amb una funcio fb(t) que inclou els dos casos
Qb=@(t,x,y) -8*x-0.19*y+fb(t);
Fb=@(t,X) [X(2);Qb(t,X(1),X(2))];
[tb,Xb,teb,Xeb]=ode45(Fb,[0,30],[0,0.70],opcions);
fprintf('teb = %.6f\n',teb)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events', @condicioE10);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('t2 = %.6f\n',te(2))
Q=@(x,y) (x-1.20).*(y+1.90);
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
Dy0=DX0(2); %com que es positiu, posarem signe=1 a la funcio condicio
fprintf('Dy0 = %.6f\n',Dy0)
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE11);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
periode=te(2); %si fem signe=0, el periode sera te(3)
fprintf('periode = %.6f\n',periode)
P=@(x,y) y+x.^2+0.11*x.^3-y.^2;
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events', @(t,X) condicioE12(t,X,Q));
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('y1 = %.6f\n',y1)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE13);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('vf = %.6f\n',vf)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE14);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('temps = %.6f\n',temps)
P=@(x,y) 3.50*(x+4*y)+x.^2+4*x.*y;
Q=@(x,y) -6*x-5*y+(6*x.*y+5*y.^2)/3.30;
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE15);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('temps = %.6f\n',temps)
Q=@(x,y) -7.37*y-5.81-6.02*sin(x);
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE16);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('xmax = %.6f\n',xmax)
A = [-1,a,b; 0,-1,1; 0,0,-3];
% Nota: Tambe podem definir les 3 components per separat,
% F=@(t,X) [P(X(1),X(2),X(3));Q(X(1),X(2),X(3));R(X(1),X(2),X(3))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE17);
[t,X,te,Xe] = ode45(F, [t0,tf], X0, opcions);
fprintf('temps = %.6f\n',temps)
g=9.80; l=0.54; %no necessitem m
F=@(t,X) [X(2);Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE18);
%obtingut aillant y=+sqrt(...) de la quantitat conservada,
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('temps = %.6f\n',temps)
F=@(t,X) [P(X(1),X(2));Q(X(1),X(2))];
opcions=odeset('AbsTol',1e-8,'RelTol',1e-8);
opcions=odeset(opcions,'Events',@condicioE19);
[t,X,te,Xe]=ode45(F,[t0,tf],X0,opcions);
fprintf('yt1 = %.6f\n',yt1)
function [expr,aturar,signe]=condicioE06(t,X,E,E0)
function [expr,aturar,signe]=condicioE07(t,X)
function [expr,aturar,signe]=condicioE08(t,X)
function [expr,aturar,signe]=condicioE09(t,X)
function [expr,aturar,signe]=condicioE10(t,X)
function [expr,aturar,signe]=condicioE11(t,X)
function [expr,aturar,signe]=condicioE12(t,X,Q)
function [expr,aturar,signe]=condicioE13(t,X)
function [expr,aturar,signe]=condicioE14(t,X)
function [expr,aturar,signe]=condicioE15(t,X)
function [expr,aturar,signe]=condicioE16(t,X)
function [expr,aturar,signe]=condicioE17(t,X)
function [expr,aturar,signe]=condicioE18(t,X)
function [expr,aturar,signe]=condicioE19(t,X)