Documente Academic
Documente Profesional
Documente Cultură
k2
k 1!
k 1
k2
.
k 1 k 1!
end
b(i)=1;
end
x=a\b;
disp(x);
//gata
a=linspace(0,1,20);
for i=1:20
x(i)=g(a(i));
end
plot(a,x)
xgrid;
xtitle('radacina in functie de a')
xi 2
b.
c.
3
i
sin xi 0
xi
2
1 x i 1 1 xi
// niste sume
clear;
n=200000;
x=10*(rand(n,1)-0.5);
tic();
//metoda 1
s=0;
for i=1:n
if(x(i)>2)
s=s+x(i)^3;
end
end
toc()
disp(s)
//metoda 2
tic()
s1=sum(x(x>2).^3)
toc()
x u 2 v2
1 u 1
,
y uv
z u 2 v2 1 v 1
Prin comenzile urmtoare vedem care este distana ntre dou numere reale
consecutive reprezentabile n calculador:
-->nearfloat("succ",10^10)-10^10
ans =
1.907348632812500D-06
-->nearfloat("succ",10^15)-10^5
ans =
9.999999999000001D+14
10. Se d tabelul:
x
y
1
-2
3
4
4
1
5
6
Pentru n=70 vedem c apar mari diferene la capete datorit erorilor de3 rotunjire:
1
5
3
4
4
0
5
-1
x 2 sin x 2
este contracie pe [-1, 1]. S se programeze
6
contor=contor+1;
disp(xi);
val=f(xi);
for i=1:n
xi(i)=xi(i)+h;
a(:,i)=(f(xi)-val)/h;
xi(i)=xi(i)-h;
end
xu=xi-inv(a)*f(xi);
er=norm(xi-xu);
xi=xu
end
disp(xi)
x 3 xy y 3 4 0
x 4 x y 4 5 0
//rezolvarea sistemelor neliniare
4 x y z 2
19. S se programeze rezolvarea sistemului x 4 y z 3 prin metoda Iacobi.
x 2 y 5 z 0
//metoda Iacobi
a=[4, -1, 1; 1,4,-1; -1,2,5]
b=[2;3;0]
D=diag(diag(a));
A=-inv(D)*(a-D)
B=inv(D)*b;
function y=f(x)
y=A*x+B
endfunction
x1=[1;1;1]
for i=1:20
x1=f(x1)
end
b.
//metoda lui Simpson
function y=f(x)
y=sin(x^2+1)/(1+sqrt(2+x))
endfunction
a=3
b=5
n=10;
x=linspace(a,b,n+1);
s1=0;
for i=2:n
s1=s1+f(x(i))
end
s2=0;
for i=1:n
s2=s2+f((x(i)+x(i+1))/2)
sin x 2 1
dx prin metoda trapezelor,
1 2x
end
rez=(f(a)+f(b)+2*s1+4*s2)*(b-a)/(6*n)
disp(rez)
c.
//utilizam procedura de integrare din scilab
function y=f(x)
y=sin(x^2+1)/(1+sqrt(2+x))
endfunction
rez=integrate('f(x)','x',3,5)
disp(rez)
x cos y x 2
21. S se calculeze
dxdy , D x, y , 0 x 2, x 1 y x 2 1 prin
2
2
1 x y
D
xy z
V x, y, z , x 2 y 2 z 1, 1 x 2 y 1 x 2 , 1 x 1
//integrala tripla
function u=f(x, y, z)
u=(x*y+z)/(1+x+y+z)
endfunction
function z=zjos(x, y)
z=x^2+y^2
endfunction
function z=zsus(x, y)
z=1
endfunction
function y=jos(x)
y=-sqrt(1-x^2)
endfunction
function y=sus(x)
y=sqrt(1-x^2)
endfunction
a=-1
b=1
function v=intz(x, y)
v=integrate('f(x,y,z)','z',zjos(x,y),zsus(x,y))
endfunction
function v=inty(x)
v=integrate('intz(x,y)','y',jos(x),sus(x))
endfunction
tic()
rez=integrate('inty(x)','x',a,b)
disp(rez)
disp(toc())
for i=1:n
yp=yepc(i)+h*f(x(i)+h/2,yepc(i)+h/2*f(x(i),yepc(i)));
yc=yepc(i)+h/2*(f(x(i),yepc(i))+f(x(i)+h,yp));
while (abs(yc-yp)/(1+abs(yp))>eps)
yp=yc;
yc=yepc(i)+h/2*(f(x(i),yepc(i))+f(x(i)+h,yp));
end
yepc(i+1)=yc;
end
plot(x,yepc,'k:');
//metoda Runge-Kutta
yrk4=zeros(1,n+1);
yrk4(1)=yi;
for i=1:n
k1=h*f(x(i),yrk4(i));
k2=h*f(x(i)+h/2,yrk4(i)+k1/2);
k3=h*f(x(i)+h/2,yrk4(i)+k2/2);
k4=h*f(x(i)+h,yrk4(i)+k3);
yrk4(i+1)=yrk4(i)+(k1+2*k2+2*k3+k4)/6;
end
plot(x,yrk4,'c-')
//y exact
yexact=zeros(1,n+1);
for i=1:n+1
yexact(i)=x(i)*exp(x(i));
end
plot(x,yexact,'b:')
xgrid
interval de lungime L=200. Programele se vor face pentru metodele: Euler, Euler
modificat, Euler mbuntit, Runge-Kutta de ordinal 4. S se fac graficul curbei
y1 x , y2 x 0 x 200 .
//metoda numerice pentru sisteme de ecuatii diferentiale
clear
nec=2;
function z=f(x, y)
z=zeros(nec,1);
z(1)=0.1*y(1)-0.01*y(1)*y(2)
z(2)=-0.3*y(2)+0.02*y(1)*y(2)
endfunction
//conditiile initiale
xi=0; yi=[20;20];
//intervalul
L=100;
n=200;
h=L/n;
x=linspace(xi,xi+L,n+1);
//metoda Euler
yeuler=zeros(nec,n+1);
yeuler(:,1)=yi;
for i=1:n
yeuler(:,i+1)=yeuler(:,i)+h*f(x(i),yeuler(:,i));
end
//metoda Euler modificata
yemodif=zeros(nec,n+1);
yemodif(:,1)=yi;
for i=1:n
yemodif(:,i+1)=yemodif(:,i)+h*f(x(i)+h/2,yemodif(:,i)+h/2*f(x(i),yemodif(:,i)));
end
//Euler imbunatatita
yeimb=zeros(nec,n+1);
yeimb(:,1)=yi;
for i=1:n
yeimb(:,i+1)=yeimb(:,i)+h/2*(f(x(i),yeimb(:,i))+f(x(i)+h,yeimb(:,i)+h*f(x(i),yeimb(:,i))));
end
//metoda Runge-Kutta
yrk4=zeros(nec,n+1);
yrk4(:,1)=yi;
for i=1:n
k1=h*f(x(i),yrk4(:,i));
k2=h*f(x(i)+h/2,yrk4(:,i)+k1/2);
k3=h*f(x(i)+h/2,yrk4(:,i)+k2/2);
k4=h*f(x(i)+h,yrk4(:,i)+k3);
yrk4(:,i+1)=yrk4(:,i)+(k1+2*k2+2*k3+k4)/6;
end
//plot(x,yrk4(1,:),'r');
//plot(x,yrk4(2,:),'b');
//plot(x,yrk4(3,:),'k')
plot(yrk4(1,:),yrk4(2,:))
xgrid
interval de lungime L=200. Programele se vor face pentru metodele AdamsBashfort i Adams-Moulton. Paii iniiali se vor calcula prin metoda RK4. S se
fac graficul curbei y1 x , y2 x 0 x 200 .
//metoda multipas pentru ecuatii diferentiale
clear
nec=2;
function z=f(x, y)
z=zeros(nec,1);
z(1)=0.1*y(1)-0.01*y(1)*y(2)
z(2)=-0.3*y(2)+0.02*y(1)*y(2)
endfunction
//conditiile initiale
xi=0; yi=[20;20];
//intervalul
L=1000;
n=20000;
h=L/n;
x=linspace(xi,xi+L,n+1);
//primii 3 pasi
ymp=zeros(nec,n+1);
ymp(:,1)=yi;
for i=1:3
k1=h*f(x(i),ymp(:,i));
k2=h*f(x(i)+h/2,ymp(:,i)+k1/2);
k3=h*f(x(i)+h/2,ymp(:,i)+k2/2);
k4=h*f(x(i)+h,ymp(:,i)+k3);
ymp(:,i+1)=ymp(:,i)+(k1+2*k2+2*k3+k4)/6;
end
//Adams Bashfort de ordinul 2
for i=4:n
ymp(:,i+1)=ymp(:,i)+h/2*(3*f(x(i),ymp(:,i))-f(x(i-1),ymp(:,i-1)))
end
yab2=ymp;
//plot(yab2(1,:),yab2(2,:));
//xgrid
//Adams Bashfort de ordinul 3
for i=4:n
ymp(:,i+1)=ymp(:,i)+h/12*(23*f(x(i),ymp(:,i))-16*f(x(i-1),ymp(:,i-1))+5*f(x(i-2),ymp(:,i-2)));
end
yab3=ymp;
//plot(yab3(1,:),yab3(2,:));
xgrid;
//Adams Bashfort de ordinul 4
for i=4:n
ymp(:,i+1)=ymp(:,i)+h/24*(55*f(x(i),ymp(:,i))-59*f(x(i-1),ymp(:,i-1))+37*f(x(i-2),ymp(:,i-2))-9*f(x(i3),ymp(:,i-3)));
end
yab4=ymp;
//plot(yab4(1,:),yab4(2,:));
//xgrid;
//Adams Moulton de ordinul 3
eps=0.0000000001;
for i=4:n
yp=ymp(:,i)+h/24*(55*f(x(i),ymp(:,i))-59*f(x(i-1),ymp(:,i-1))+37*f(x(i-2),ymp(:,i-2))-9*f(x(i-3),ymp(:,i-3)));
yc=ymp(:,i)+h/12*(5*f(x(i+1),yp)+8*f(x(i),ymp(:,i))-f(x(i-1),ymp(:,i-1)));
while (abs(yc-yp)/(1+abs(yp))>eps)
yp=yc;
yc=ymp(:,i)+h/12*(5*f(x(i+1),yp)+8*f(x(i),ymp(:,i))-f(x(i-1),ymp(:,i-1)));
end
ymp(:,i+1)=yc;
end
yam3=ymp;
//plot(yam3(1,:),yam3(2,:));
xgrid
//Adams Moulton de ordinul 4
eps=0.0000000001;
for i=4:n
yp=ymp(:,i)+h/24*(55*f(x(i),ymp(:,i))-59*f(x(i-1),ymp(:,i-1))+37*f(x(i-2),ymp(:,i-2))-9*f(x(i-3),ymp(:,i-3)));
yc=ymp(:,i)+h/24*(9*f(x(i+1),yp)+19*f(x(i),ymp(:,i))-5*f(x(i-1),ymp(:,i-1))+f(x(i-2),ymp(:,i-2)));
while (abs(yc-yp)/(1+abs(yp))>eps)
yp=yc;
yc=ymp(:,i)+h/24*(9*f(x(i+1),yp)+19*f(x(i),ymp(:,i))-5*f(x(i-1),ymp(:,i-1))+f(x(i-2),ymp(:,i-2)));
end
ymp(:,i+1)=yc;
end
yam4=ymp;
plot(yam4(1,:),yam4(2,:));
xgrid