0% found this document useful (0 votes)
77 views

Metode Numerice: Academia Tehnica Militara

The document discusses several numerical methods for solving equations including: 1. Bisection method and secant method for solving algebraic equations. Example algorithms and outputs are provided. 2. Gauss-Seidel and Newton-Raphson methods for solving linear equations. Example algorithms and outputs are provided. 3. Power method, linear interpolation, Lagrange interpolation, linear regression, and quadratic regression for other applications. Example algorithms and outputs are provided.

Uploaded by

Ionut Predescu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Metode Numerice: Academia Tehnica Militara

The document discusses several numerical methods for solving equations including: 1. Bisection method and secant method for solving algebraic equations. Example algorithms and outputs are provided. 2. Gauss-Seidel and Newton-Raphson methods for solving linear equations. Example algorithms and outputs are provided. 3. Power method, linear interpolation, Lagrange interpolation, linear regression, and quadratic regression for other applications. Example algorithms and outputs are provided.

Uploaded by

Ionut Predescu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Academia Tehnica Militara

METODE NUMERICE

Stud. Cap. Ionut-Catalin Predescu


Grupa M-192

Academia Tehnica Militara

I.

Ecuatii algebrice
1. Metoda injumatatirii intervalului(bisectiei)

Date de intrare:
-functie: (x-2)*cos(x);
-interval: [2,4];
-precizia: 0.00001
Algoritmul in Matlab:
function bisectie(a,b,eps)
fid=fopen('bisectie.txt','w');
fprintf(fid, 'iteratia Aproximare f(Aproximare) \n');
f=@(x) (x-2)*cos(x);
i=1;
fprintf(fid, '%3.0f %12.9f %12.9f \n',i,(a+b)/2,f((a+b)/2));
while b-a>eps
i=i+1;
if f(a)*f((a+b)/2)<0
b=(a+b)/2;
else
a=(a+b)/2;
end
fprintf(fid, '%3.0f %12.9f %12.9f \n',i,(a+b)/2,f((a+b)/2));
end
fclose(fid);
end

Date de iesire:
1

3.000000000 -0.989992497
3.500000000 -1.404685031
3.750000000 -1.435978875
3.875000000 -1.392933422
3.937500000 -1.355546215
3.968750000 -1.332786093
3.984375000 -1.320380086
3.992187500 -1.313919639
3.996093750 -1.310624949
3.998046875 -1.308961474
3.999023438 -1.308125703
3.999511719 -1.307706808
3.999755859 -1.307497109
3.999877930 -1.307392196
3.999938965 -1.307339724
3.999969482 -1.307313484
3.999984741 -1.307300363

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Academia Tehnica Militara

2.Metoda coardei(secantei)

Date de intrare:
-functie: (x-2)*cos(x);
-interval: [2,4];
-precizia: 0.00001;
Algoritmul in Matlab:
function coarda(a,b,eps)
fid=fopen('coarda.txt','w');
fprintf(fid,'iteratia Aproximarea f(Aproximare) \n');
f=@(x) cos(2*x)-x-2;
x1=(a*f(b)-b*f(a))/(f(b)-f(a));
i=1;
fprintf(fid,'%3.0f %12.9f %12.9f \n',i,x1,f(x1));
if f(a)*f(x1)<0
b=x1;
else
a=x1;
end
x2=(a*f(b)-b*f(a))/(f(b)-f(a));
i=2;
fprintf(fid,'%3.0f %12.f %12.f \n',i,x2,f(x2));
while abs(x2-x1)>eps
x1=x2;
i=i+1;
if f(a)*f(x1)<0
b=x1;
else
a=x1;
end
x2=(a*f(b)-b*f(a))/(f(b)-f(a));
fprintf(fid,'%3.0f %12.9f %12.9f \n',i,x2,f(x2));
end
fclose(fid);
end

Date de iesire:
1 -4.238728581 1.654868278
2
-3
1
3 -1.738876201 -1.205152184
4 -2.195258596 -0.121084181
5 -2.236468432 -0.000701979
6 -2.236707188 0.000000691
7 -2.236706953 -0.000000000

Academia Tehnica Militara

3.Metoda tangentei de ordinu I

Algoritmul
function tangenta(x0,eps)
fid=fopen('tangenta.txt','a+');
fprintf(fid,'Iteratia Solutia |f(sol)| \n');
i=1;
fprintf(fid,'%3.0f %15.12f %15.12f \n',i,x0,abs(f(x0)));
x1=x0-f(x0)/df(x0);
i=i+1;
fprintf(fid,'%3.0f %15.12f %15.12f \n',i,x1,abs(f(x1)));
while abs(x1-x0)>eps
i=i+1;
x0=x1;
x1=x0-f(x0)/df(x0);
fprintf(fid,'%3.0f %15.12f %15.12f \n',i,x1,abs(f(x1)));
end
fclose(fid);
end

Apelarea functiei:
tangenta(1,10^-5)
Date de iesire:
Iteratia Solutia |f(sol)|
Iteratia Solutia |f(sol)|
Iteratia Solutia |f(sol)|
Iteratia Solutia |f(sol)|
1 1.000000000000 0.200000000000
2 0.976190476190 0.006808762810
Iteratia Solutia |f(sol)|
1 1.000000000000 0.200000000000
2 0.976190476190 0.006808762810
3 0.975321184953 0.000008787342
Iteratia Solutia |f(sol)|
1 1.000000000000 0.200000000000
2 0.976190476190 0.006808762810
3 0.975321184953 0.000008787342
4 0.975320060149 0.000000000015

Academia Tehnica Militara

II.

Ecuatii liniare

Metoda Gauss-Seidel

Date de intrare:
A=[1 -1 4 3 6 2;2 5 3 4 2 1;4 2 6 1 -1 -2;-4 4 4 1 1 -3;2 2 -2 3 3 4;-1-2 3 4 1
1 -2];
b=[-1;2;-2;3;-2;2];
>> eig(A);
>> Gauss_Seidel(A,b,20)
Algoritmul in Matlab:
function Gauss_Seidel(A,b,nriter)
[m,n]=size(A);
D=zeros(n);
for i=1:n
D(i,i)=A(i,i);
end
fid=fopen('Gauss_Seidel.txt','w');
fprintf(fid,'iteratia Solutia Aproximativa \n');
x0=zeros(n,1); x1=zeros(n,1);
if det(D)==0
display('Metoda Gauss_Seidel nu se poate aplica,ekemente 0 pe diagonala
matricei');
else
display('Metoda Gauss_Seidel se aplica');
for k=1:nriter
x1(1)=b(1)/A(1,1);
for j=2:n
x1(1)=x1(1)-A(1,j)/A(1,1)*x0(j);
end
for i=2:n-1
x1(i)=b(i)/A(i,i);
for j=1:i-1
x1(i)=x1(i)-A(i,j)/A(i,i)*x1(j);
end
for j=i+1:n
x1(i)=x1(i)-A(i,j)/A(i,i)*x0(j);
end
end
x1(n)=b(n)/A(n,n);
for j=1:n-1
x1(n)=x1(n)-A(n,j)/A(n,n)*x1(j);

Academia Tehnica Militara


end
fprintf(fid,'%3.0f',k);
for i=1:n
fprintf(fid,'%10.7f',x1(i));
end
fprintf(fid,'\n');
x0=x1;

end
x=A^(-1)*b;%solutia exacta
fprintf(fid,'Solutia exacta: ');
for i=1:n
fprintf(fid,'%10.7f',x(i));
end

end
fclose(fid);
end

Date de iesire:
Solutia exacta: -1.4077253-2.4892704-0.7768240 6.6480687-2.4592275-2.0815451

Metoda Newton-Raphson

Algoritm in Matlab:
function [sol_x sol_y]=Newton_Raphson(x0,y0,nriter)
syms x y
f1= 2*x^3-3*y;
f2= x^2+3*y^2+3*x;
df1=[diff(f1,x) diff(f1,y)];
df2=[diff(f2,x) diff(f2,y)];
X0=[x0;y0];%vectorul lui X
F=[f1;f2];%vectorul functiei
dF=[df1;df2];%matricea Jacobiana
for k=1:nriter
A=subs(dF,[x;y],X0);
X1=X0-A^(-1)*subs(F,[x;y],X0);
sol_x(k)=X1(1);
sol_y(k)=X1(2);
X0=X1;
k
end
end

Apelare functie:

Academia Tehnica Militara

[sol_x sol_y]=Newton_Raphson(1,2,10)
Date de iesire:
sol_x =[ 1, 23/39, 128287/2727855,
87668581303497964856234021/1538714660083476233929260885, .]
sol_y =[ 2/3, -2/13, -5911046/24550695,
104638031024853290865538/923228796050085740357556531, ]

Metoda puterii
Date de intrare:
ordinul matricii=2
A={5,3 ; 2,1}
Apelarea functiei:
n=2;
>> A=[5 3;2 1];
>> [valp,vectp]=metoda_puterii(A,ones(n,1),0.0001,15)
Algoritmul in Matlab:
function [valp,vectp]=metodaputerii(A,x0,tol,maxiter)
z0=x0/norm(x0);
i=1;
z1=A*z0/norm(A*z0);
while i<=maxiter && (1-abs(z1'*z0)>tol)
i=i+1;
z0=z1;
z1=A*z0/norm(A*z0);
end
if i>maxiter
display('s-a atins nr maxim de iteratii fara a se fi atins nivelul de
toleranta prescris');
valp=0; vectp=o;
else
vectp=z1;
valp=z1'*A*z1;
end
end

Academia Tehnica Militara

Date de iesire:
valp =

6.1626
vectp =

0.9324
0.3615
Interpolarea liniara pe portiuni

Apelarea functiei:
x=[-3 1 -2 -2 3 6 1];
y=[3 4 2 -2 -6 2 1];
f=interpliniara(x,y,-1.5)
Algoritm in Matlab:
function f=interpliniara(x,y,alfa)
n=length(x);
m=length(y);
if n~=m
display('date incorecte.Lungimi de vectori diferite');f=0;
else
if alfa<x(1) || alfa>x(n)
display('date incorecte.Alexere gresita pt alfa');f=0;
else
i=1;
while alfa>=x(i)
i=i+1;
end
f=y(i-1)+(y(i)-y(i-1))/(x(i)-x(i-1))*(alfa-x(i-1));

Academia Tehnica Militara

plot(x,y,alfa,f,'o','Linewidth',2,'Markersize',10,'MarkerEdgeColor','k','Marke
rFaceColor','m');
end
end
end

Date de iesire:
f =3.3750
Grafic:

Interpolarea folosind polinomul lui Lagrange

Apelarea functiei:
x=[2 3 5 -1 4 -2 1];

Academia Tehnica Militara

>> y=[-2 1 -3 -6 3 4 2];


>> f=Lagrange(x,y,2)
Algoritmul in Matlab:

function f=Lagrange(x,y,alfa)
n=length(x);
m=length(y);
if n~=m
display('Date incorecte.Lungimi diferite de vectori');f=0;
else
syms X
Lag=0;
for i=1:n
pol=1;
for j=1:n
if j~=i
pol=pol*(X-x(j))/(x(i)-x(j));
end
end
Lag=Lag+y(i)*pol;
end
u=linspace(x(1),x(n),100);
v=zeros(1,100);
for i=1:100
v(i)=subs(Lag,X,u(i));
end
figure
plot(u,v,'Linewidth',2);
hold on
plot(x,y,'o','MarkerSize',4,'MarkerEdgeColor','k','MarkerFace','g');
plot(alfa,subs(Lag,X,alfa),'o','MarkerSize',10,'MarkerEdgeColor','k','MarkerFa
ce','m');
f=subs(Lag,X,alfa);
end
end

Date de iesire: f =-2

Academia Tehnica Militara

Regresia liniara
Apelarea functiei:
x=[2 3 5 -1 4 -2 1];
y=[-2 1 -3 -6 3 4 2];
[a,b,f]=regresieliniara(x,y,2)
Algoritmul in Matlab:
function [a,b,f]=regresieliniara(x,y,alfa)
n=length(x);
m=length(y);
if n~=m
display('Date incorecte. Lungimi de vectori diferite');
a=-inf; b=-inf; f=-inf;
else
A=[sum(x.^2) sum(x);sum(x) n];
B=[sum(x.*y);sum(y)];
Z=A^(-1)*B;
a=Z(1); b=Z(2); f=a*alfa+b;
u=linspace(min(min(x),alfa),max(max(x),alfa),200);
v=a*u+b;
figure
plot(u,v)
hold on
plot(x,y,'o','MarkerSize',1);
plot(alfa,f,'o','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor','m');
end
end

Date de iesire: a=-0.0580; b=-0.0435; f=-0.1594

Academia Tehnica Militara

Regresia parabolica
Apelarea functiei:
n=350;
x=randn(n,1);
a=1+0.25*randn(n,1);
b=1+0.45*randn(n,1);
c=1+0.37*randn(n,1);
y=1+a.*(x.^2)-2*b+3*c;
[a,b,c,f]=regresieparabolica(x,y,1.2)
Algoritmul in Matlab
function [a,b,c,f]=regresieparabolica(x,y,alfa)
n=length(x);
m=length(y);
if n~=m
display('Date incorecte.Lungimi de vectori diferite');
a=0;b=0;c=0;f=0;
else
A=[sum(x.^4) sum(x.^3) sum(x.^2); sum(x.^3) sum(x.^2) sum(x); sum(x.^2)
sum(x) n];
B=[sum((x.^2).*y); sum(x.*y); sum(y)];
Z=A^(-1)*B;
a=Z(1);b=Z(2);c=Z(3);
f=a*alfa^2+b*alfa+c;
u=linspace(min(min(x),alfa),max(max(x),alfa),200);
v=a*u.^2+b*u+c;
figure
plot(u,v)
hold on
plot(x,y,'o','MarkerSize',1);
plot(alfa,f,'o','MarkerSize',5,'MarkerFaceColor','m');
end

Academia Tehnica Militara


end

Date de iesire:
a = 1.0995; b =-0.1125; c =1.8814; f =3.3297

Metoda Euler pentru rezolvarea ecuatiilor diferentiale


Apelarea functiei:
y1=euler(0,1,15,0)
syms x y;
sol=dsolve('Dy=x^2+y-1','y(0)=0','x');
subs(sol,x,1);
vpa(y1,15);
vpa(subs(sol,x,1),15);
t=linspace(0,1,100);
y=zeros(100,1);
y(1)=0;
for i=2:100;
y(i)=euler(0,t(i),200,0);
end
z(1)=0;
for i=2:100;
z(i)=subs(sol,x,t(i));
end
plot(t,y,t,z)
Algoritmul in Matlab:
function y1=euler(x0,x1,n,y0)
%y0 este sol initiala pt x
%y1 este sol aproxmativa x1
%x0 este val initiativa pt x
%x1 este val finala pt x
% n nr de pasi
h=(x1-x0)/n;

Academia Tehnica Militara


x=zeros(n+1,1);
y=zeros(n+1,1);
x(1)=x0;
y(1)=y0;
for i=2:n+1
x(i)=x(i-1)+h;
end
syms X Y
f=Y+X^2;
for k=2:n+1
y(k)=y(k-1)+h*subs(f,[X Y],[x(k-1),y(k-1)]);
end
y1=y(n+1);
end

You might also like