Sunteți pe pagina 1din 25

1.

metoda aproximarilor succesive de determinare a solutiilor unei ecuatii transcendente


function[x,nrit]=aproxsuc(nume_f,x0,prec);
x1=x0+2*prec;
nrit=0;
while abs(x1-x0)>prec
x1=x0;
x0=feval(nume_f,x1)+x1;
nrit=nrit+1;
end;
x=x0;
%exemplul aplicativ folosit este ecuatia urmatoare:f1(x)=1/x^2+1/x+1-x=0
2.metoda bisectiunilor succesive de rezolvare a unei ecuatii algebrice sau transcendente
despre care se stie ca
% are o singura solutie intr un interval dat ,[a,b];
% metoda de mai jos e modificata pt a corespunde ecuatiei transcendente f2(x)=exp(x)-5*x;

function [x,nrit]=bissuc(a,b,prec)
if(f2(a)*f2(b))<=0
if f2(a)==0
x=a;
elseif f2(b)==0
x=b;
else nrit=0;

while abs(a-b)>prec
c=(a+b)/2;
if f2(c)==0
x=c;
elseif f2(a)*f2(c)<0
b=c;
else a=c;
end;
x=(a+b)/2;
nrit=nrit+1;
end;
end;
else
s=sprintf('In intervalul[%6.3f;%6.3f]nu e o singura solutie',a,b);
disp(s);
x=2000;
nrit=0;
end;
% apelul metodei pt functia data f2(x) este urmatorul: >>
[sol,nrit]=bissuc(0,1,1e-6)

% pt a reda grafic locul radacinilor si graficul functiei scriem in command window

% >> x=-3:0.01:4;
% >> for i=1:length(x) y(i)=f2(x(i));end;
% >> plot(x,y,'r-', 0.2592,f2( 0.2592),'b*',[-3,4],[0 0],'k') %afiseaza prima
radacina
% >> plot(x,y,'r-', 0.2592,f2( 0.2592),'b*',[-3,4],[0
0],'k',2.542564,f2(2.542564),'mo') afiseaza ambele radacini prima noatat cu * iar a doua o
3. metoda de aproximare prin regresie exponentialaa unui set de date (x,y)
% curba de regresie are ecuatia y=e^ax+b;
% x,y au aceeasi dimensiune si y are valori pozitive

function[A,B,E,xx,yy]=cmmp_exp(x,y)

z=log(y);

[A,B,E1,y_aprox1]=cmmp_lin(x,z);

xx=min(x):(max(x)-min(x))/200:max(x);
yy=exp(A*xx+B);

E=sum((y-exp(A*x+B)).^2);
% pt a implementa metoda avem modelul x=[-3 -2 0 0.5 1.5 3 4];
% >> y=[0.2725 0.4493 1.2214 1.5683 2.5857 5.4739 9.0250]; unde y e genertat de
y=exp(0.5*x+0.2);
% apelul functiei este >> [A,B,E]=cmmp_exp(x,y)
% introducem o perturbatie de aproximativ de 25% si avem astfel vectorul y1=[0.2998
0.4044 1.3435 1.4115 2.8443 4.9266 9.9275]
% apelul e >> [A,B,E]=cmmp_exp(x,y1)

%pt a evidentia in paralel aproximarile parabolica si exponentiala a unui nor de puncte


putem face un plot
% >> [A,B,E1,xx1,yy1]=cmmp_exp(x,y1);
% >> [a,b,c,E2,xx2,yy2]=cmmp_par(x,y1);
% plot(x,y,'ko',xx1,yy1,'k--',xx2,yy2,'k-');grid;
4. metoda cmmp de aproximare a dependentei y=f(x)
%printr un polinom de gradul m-1(cu m parametrii)
%cazul general de regresie polinomiala
%prezentarea in ordinea crescatoare a puterilor variabilei

function [coef,E,y_aprox,xx,yy]=cmmp_gen(x,y,m)
n=length(x);
for j=1:m
t(j)=0;
for i=1:m
s(j,i)=0;
for k=1:n
s(j,i)=s(j,i)+x(k)^(j+i-2);
end;
end;
for k=1:n
t(j)=t(j)+y(k)*(x(k)^(j-1));
end;
end;

coef=inv(s)*t';
coef=coef';
for k=1:n
y_aprox(k)=0;
for i=1:m
y_aprox(k)=y_aprox(k)+coef(i)*(x(k)^(i-1));
end;
end;
E=(y-y_aprox)*(y-y_aprox)';

%pentru reprezentarea grafica folosim urmatoarele instructiuni


xx=min(x):(max(x)-min(x))/200:max(x);
for k=1:length(xx)
yy(k)=0;
for i=1:m
yy(k)=yy(k)+coef(i)*(xx(k)^(i-1));
end;
end;
% apelul functiei este

% >> x=[3 3.3 3.7 3.9 4.7 4.8 4.9 5 5.25];


% >> y=[3 5 10 14 43 49 56 63 84];
% >> [coef,E]=cmmp_gen(x,y,length(x)-1)

5. metoda cmmp de aproximare a dependentei y=f(x)


%printr un polinom de gradul m-1(cu m parametrii)
%cazul general de regresie polinomiala
%prezentarea in ordinea descrescatoare a puterilor variabilei

function [coef,E,y_aprox,xx,yy]=cmmp_gen(x,y,m)
n=length(x);
for j=1:m
t(j)=0;
for i=1:m
s(j,i)=0;
for k=1:n
s(j,i)=s(j,i)+x(k)^(j+i-2);
end;
end;
for k=1:n
t(j)=t(j)+y(k)*(x(k)^(j-1));
end;
end;
coef=inv(s)*t';
coef=fliplr(coef');
for k=1:n
y_aprox(k)=polyval(coef,x(k));
end;
E=(y-y_aprox)*(y-y_aprox)';
%pentru reprezentarea grafica folosim urmatoarele instructiuni
xx=min(x):(max(x)-min(x))/200:max(x);
for k=1:length(xx)
yy(k)=polyval(coef,xx(k));
end;
% apelul functiei
% >> x=[3 3.3 3.7 3.9 4.7 4.8 4.9 5 5.25];
% >> y=[3 5 10 14 43 49 56 63 84];
% >> [coef,E]=cmmp_gen1(x,y,length(x)-1)
6. metoda ce implementeaza regresia liniara pe un set de perechie de date(x,y)
%dreapta de regresie are ecuatia y=a*x+b\
% vectorii x,y au aceeasi dimensiune
function [a,b,y_aprox,E]=cmmp_lin(x,y)
n=length(x);
Sx=sum(x);
Sxx=sum(x.^2);
Sy=sum(y);
Syy=sum(x.*y);
d=n*Sxx-Sx^2;
da=n*Syy-Sx*Sy;
db=Sxx*Sy-Sx*Syy;
a=da/d;
b=db/d;
y_aprox=a*x+b;
E=sum((y-y_aprox).^2);

% implementarea se face astfel in linia de comanda


% introducem vectorul si dreapta de regresie
% >> x=[1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6];y=2*x+7;
% >> [a,b,y_aprox,E]=cmmp_lin(x,y)
% daca vrem sa perturbam valorile obtinute cu y=2*x+7(de exemplu cu maxim 10%)
% rezulta y1=[8.1 11 9.9 13.2 11.7 15.4 13.5 17.6 15.3 19.8 17.1]
% apelul functiei va fi [a1,b1,y1_aprox,E1]=cmmp_lin(x,y1)
% plotul asociat regresiei este
% xx=min(x):(max(x)-min(x))/200:max(x);
% yy=a*xx+b;
% plot(x,y,'o',xx,yy,'-');
% grid;

7. metoda ce implementeaza regresia parabolica pe perechea de vectori de aceeasi


dimensiune (x,y)
%curba de regresie are ecuatia y=a*x^2+b*x+c
function[a,b,c,E,xx,yy]=cmmp_par(x,y)
n=length(x);

Sx=sum(x);
Sxx=sum(x.^2);
S3x=sum(x.^3);
S4x=sum(x.^4);
Sy=sum(y);
Sxy=sum(x.*y);
S2xy=((x.^2).*y);
d=n*Sxx*S4x-Sx^2*S4x-n*S3x^2+2*Sx*Sxx*S3x-Sxx^3;
da=n*Sxx*S2xy-Sx^2*S2xy-n*S3x*Sxy+Sx*S3x*Sy+Sx*Sxx*Sxy-Sxx^2*Sy;
db=n*S4x*Sxy-Sx*S4x*Sy-n*S3x*S2xy+Sx*Sxx*S2xy+Sxx*S3x*Sy-Sxx^2*Sxy;
dc=Sxx*S4x*Sy-Sx*S4x*Sxy-S3x^2*Sy+Sxx*S3x*Sxy+Sx*S3x*S2xy-Sxx^2*S2xy;
a=da/d;
b=db/d;
c=dc/d;
%vectorii pt o eventuala reprezetare grafica
xx=min(x):(max(x)-min(x))/200:max(x);
yy=a*xx.^2+b*xx+c;
E=sum((y-a*x.^2-b*x-c).^2);
% pt implementare am ales exemplul urmator
% >>x=[0 0.3 1 1.4 1.6 2 2.1 2.4 2.8 3 3.5 3.7 3.9 4];y=x.^2-4*x-10;
% >> [a,b,c,E]=cmmp_par(x,y)
8. functie pt calculul polinomuli derivat al unui polinom dat
function[coef_der]=der_pol(coef)
m=length(coef);
for k=1:m-1
coef_der(k)=(m-k)*coef(k);
end;
9. functia ce implementeaza jacobianul sitemului de rezolvat
%in cazul de fata forma sa este urmatoarea modificarile pt cazul altor sisteme fiind
evidente
%f1(x1,x2,x3)=x1^2+4x2^2+x3^2-2=0
%f2(x1,x2,x3)=6x1+3x2^2+x3^3-7=0
%f3(x1,x2,x3)=x1-5x2-3x3^2+2=0

function J=dF(x)
J=[2*x(1) 8*x(2) 2*x(3);6 6*x(2) 3*x(3)^2;1 -5 -6*x(3)];
10. functia F(x) pt rezolvarea unui sistem de ecuatii dat (a se tine cont de coeficientii
pt modificat in cazul alegerii unui nou sistem de rezolvat)
%in cazul nostru avem n=3 forma sistemului fiind evidenta
%f1(x1,x2,x3)=x1^2+4x2^2+x3^2-2=0
%f2(x1,x2,x3)=6x1+3x2^2+x3^3-7=0
%f3(x1,x2,x3)=x1-5x2-3x3^2+2=0
function y=F(x)
y(1)=x(1)^2+4*x(2)^2+x(3)^2-2;
y(2)=6*x(1)+3*x(2)^2+x(3)^3-7;
y(3)=x(1)-5*x(2)-3*x(3)^2+2;
y=y';
% transpunerea e necesara pt returnarea unui vector coloana
11.functia pt verificarea metodei aproximarilor succesive a solutiilor unei ec
transcendente
function y=f1(x)
y=1/x^2+1/x+1-x;
%apelul functiei pt ecuatia data este urmatorul >> [sol,iter]=aproxsuc('f1',5,1e-4)
12. functie pt verificarea metodei tangentei a carei derivata este fder
%functie este f2(x)=exp(x)-5*x;
function y=f2(x)
y=exp(x)-5*x;
%apelul metodei pt functia data este >> [x,iter]=met_tang('f2','fder',2,1e-6)
% apelam aceasta functie ca parametru si pt metoda bisectiunilor succesive
13. functie pt verificarea algoritmului de optimizare prin gradient
%vom afla minimul
function y=f3(x)
y=5*x^2+2*x+13;
14. functie de doua argumente reale pt exemplificarea metodei relaxarii
function y=f4(x)
y=4+6*x(1)-4*x(2)+x(1)^1+2*x(2)^2-6*x(1)*x(2)+x(1)^4+2*x(1)^2*x(2);
15.implicatii ale FSH
% program ce implementeaza factorizarea Crout fara pivotare
n=size(a);
u=eye(n);
for k=1:n
for i=1:n
l(i,k)=0;
for m=1:k-1
l(i,k)=l(i,k)+l(i,m)*u(m,k);
end;
l(i,k)=a(i,k)-l(i,k);
end;
for j=k+1:n
u(k,j)=0;
for m=1:k-1
u(k,j)=u(k,j)+l(k,m)*u(m,j);
end;
u(k,j)=(a(k,j)-u(k,j))/l(k,k);
end;
end;
% verificarea se face dand o matrice a si apoi verificand l*u=a;
16.implicatii ale FSH
% program ce implementeaza factorizarea Crout pt cazul in care eliminarea gaussiana este
cu pivotare partiala
a1=a;
n=size(a);
u=eye(n);
p=eye(n);
for k=1:n
q=k;
for i=k+1:n
if abs(a(q,k))<abs(a(i,k))
q=i;
end;
end;
if q~=k
p1=eye(n);
p1(k,k)=0;
p1(q,q)=0;
p1(q,k)=1;
p1(k,q)=1;
a=p1*a;
p=p1*p;
end;
for i=k:n
l(i,k)=0;
for m=1:k-1
l(i,k)=l(i,k)+l(i,m)*u(m,k);
end;
l(i,k)=a(i,k)-l(i,k);
end;
for j=k+1:m
u(k,j)=0;
for m=1:k-1
u(k,j)=u(k,j)+l(k,m)*u(m,j);
end;
u(k,j)=(a(k,j)-u(k,j))/l(k,k);
end;
end;
%verificarea se face dand de la linia de comanda matricea a si se verifica p*a=l*u
(P*A=L*U);
17.functii de interpolare din biblioteca matlab
x=[-2 0 5 7 7.5 9 12];
y=[-3 2.5 -5 4.8 7.3 6 2.1];
xx=min(x):0.05:max(x);
yy1=interp1(x,y,xx);
% metoda implicita e cea liniara
yy2=interp1(x,y,xx,'spline');
yy3=interp1(x,y,xx,'cubic');
plot(x,y,'ko',xx,yy1,'k-',xx,yy2,'k--',xx,yy3,'k:');grid;
18. functie pt testul metodei ce implementeaza metoda tangentei de aflarea solutiei ec
algebrice si transcendente
function [y]=fder(x)
y=exp(x)-5;
%apell metodei pt functia data este >> [x,iter]=met_tang('f2','fder',2,1e-6)
19. metode indirecte de optimizare parametrica
%algoritmul Fletcher-Reeves(a gradientilor conjugati)
% initializari
i=1;
x(i,:)=x0;
n=length(x0);
itermax=1000;
h=0.0001;
for k=1:n
v=zeros(1,n);
v(k)=1;
grad(i,k)=(f3(x0+h/2*v)-f3(x0-h/2*v))/h;
end;
p(i,:)=-grad(i,:);
stop=0;
while(~stop)&(i<=itermax)
alfa=0:0.01:10;
y_min=f3(x(i,:));
stop=1;
for j=1:length(alfa)
y=f3(x(i,:)+alfa(j)*p(i));
if y<y_min
y_min=y;
x(i+1,:)=x(i,:)+alfa(j)*p(i);
stop=0;
end;
end;
if(~stop)
for k=1:n
v=zeros(1,n);
v(k)=1;
grad(i+1,k)=(f3(x(i+1,:)+h/2*v)-f3(x(i+1,:)-h/2*v))/h;
end;
p(i+1,:)=-grad(i+1,:)+norm(grad(i+1,:))^2/norm(grad(i,:))^2*p(i,:);
end;
i=i+1;
end;
[nr_it n]=size(x);nr_it
minim=x(nr_it,:)
%pt executia programului din linia de comanda introducem x0=1; si apelam>>fletreev.m
20. scriptul de apel al functiei ce deriveaza un polinom dat
% este o varianta a algoritmului Fletcher Reeves
% scriptul e destinat EXCLUSIV optimizarilor functiilor de o singura variabila reala
I=1;
x(i)=x0;
n=length(x0);
[coef_der]=der_pol(coef);
h=0.0001;
itermax=100;
grad(i)=polyval(coef_der,x0);
p(i)=-grad(i);
stop=0;
while (~stop)&(i<=itermax)
alfa=0:0.0001:1;
y_min=polyval(coef,x(i));
stop=1;
for j=1:length(alfa)
y=polyval(coef,x(i)+alfa(j)*p(i));
if y<y_min
y_min=y;
x(i+1)=x(i)+alfa(j)*p(i);
stop=0;
end;
end;
if(~stop)
grad(i+1)=polyval(coef_der,x(i+1));
p(i+1)=-grad(i+1)+grad(i+1)^2/grad(i)^2*p(i);
end;
i=i+1;
end;
nr_it=length(x);nr_it
minim=x(nr_it)
%pt executie introducem in linia de comanda x0=-1; si apelam grco_pol apoi clear x si
x0=2;grco_pol se observa ca pozitiile punctelor de minim sunt simetrice
21.interpolarea functiilor
% formula de interpolare Lagrange
% initializare(pt rulat F5)
x=[1 5 8 9 10];
y=[2 30 11 4 8];
[m n]=size(x);
[xsort,ind]=sort(x);
l=1;
for i=1:n
ysort(l)=y(ind(i));
l=l+1;
end;
x=xsort;
y=ysort;
L=zeros(1,n);
for i=1:n
cs=poly([x(1:i-1) x(i+1:n)]);
cp=cs*y(i)/polyval(cs,x(i));
L=L+cp;
end;
pas=(max(x)-min(x))/250;
xx=min(x):pas:max(x);
for i=1:length(xx)
yy(i)=polyval(L,xx(i));
end;
val=input('introduceti valoarea in care doriti interpolarea');
yval=polyval(L,val);
% reprezentarea grafica
plot(xx,yy,'r-',x,y,'g*');grid;
s1=sprintf('INTERPOLARE PRIN POLINOM LAGRANGE');
title(s1);
xlabel('x');
ylabel('y');
22. Interpolarea functiilor
%implementarea formulei de interpolare cu polinom Newton
% initializare exemplu(pt rezultat F5)
x=[1 5 8 9 10];
y=[2 30 11 4 8];
n=length(x);
for k=1:(n-1)
D(k,1)=(y(k+1)-y(k))/(x(k+1)-x(k));
end;
for l=2:n-1
for k=l:(n-1)
D(k,l)=(D(k,l-1)-D(k-1,l-1))/(x(k+1)-x(k-l+1));
end;
end;

tt=fliplr(diag(D)');

coef=[zeros(1,n-1) y(1)];
for i=1:(n-1)
coef=coef+[zeros(1,i-1) poly(x(1:n-i))*tt(i)];
end;
%construirea vectorilor pt reprezentarile grafice
pas=(max(x)-min(x))/250;
xx=min(x):pas:max(x);
for i=1:length(xx)
yy(i)=polyval(coef,xx(i));
end;
plot(x,y,'mo',xx,yy,'r-');
grid;
title('INTERPOLARE PRIN POLINOM NEWTON');
23. metoda Bernoulli de determinare a solutiei de modul maxim a unei ecuatii algebrice
% metoda e aici implementata pt ecuatia x^5+5x^4+x^3+x^2+5=0
%ptalte ecuatii se pot modifica usor coeficientii
a=[1 5 1 1 0 5];
n=length(a)-1;
y=[0 1 0 0 1];
m=70;
for i=1:m
y(i+n)=0;
for j=0:(n-1)
y(i+n)=y(i+n)-y(i+j)*a(n-j+1)/a(1);
end;
end;
x=y(m+n)/y(m+n-1);
s=sprintf('solutia de modul maxim este %6.4f.',x);
disp(s);
24. Simularea sistemelor cu parametrii distribuiti.Integrarea numerica a ecuatiilor
diferentiale cu derivate partiale
%Metode explicite :DuFort & Frankel
%transferul unidirectional de caldura
function[tmp1,tmp2]=met_exp1(a,L,n,t0,tf,teta0,tetaL,temp0)
dx=L/(n+1);
sigma=1/6;
dt=sigma*dx^2/a;
t=t0:dt:tf;
m=length(t);
tmp1=[teta0];
for j=2:n+1
tmp1=[tmp1 temp0];
end;
tmp1=[tmp1 tetaL];
tmp2=tmp1;
for i=2:m
temp_i1=[teta0];
temp_i2=[teta0];
for j=2:n+1
teta1=sigma*(tmp1(i-1,j-1)+tmp1(i-1,j+1))+(1-2*sigma)*tmp1(i-1,j);
if (i==2) teta=temp0;
else teta=tmp2(i-2,j);
end;
teta2=(dx^2-2*a*dt)/(dx^2+2*a*dt)*teta+2*a*dt/(dx^2+2*a*dt)*(tmp2(i-1,j-1)+tmp2(i-
1,j+1));
temp_i1=[temp_i1 teta1];
temp_i2=[temp_i2 teta2];
end;
temp_i1=[temp_i1 tetaL];
temp_i2=[temp_i2 tetaL];
tmp1=[tmp1;temp_i1];
tmp2=[tmp2;temp_i2];
end;
plot(t,tmp1,'k-',t,tmp2,'k--');
grid;
xlabel('timp[s]');
ylabel('temperatura[grade celsius]');
title('Transfer unidirectional de caldura');
25. metoda explicita
% metoda explicita DuFort&Frankel
%Simularea sistemelor cu parametrii distribuiti.Integrarea numerica a ecuatiilor
diferentiale cu derivate partiale
%Metode implicite: Cranck-Nicholson si Laasonen
%transferul unidirectional de caldura
function[tmp1,tmp2]=met_imp1(a,L,n,t0,tf,teta0,tetaL,temp0)
%initializari generale
teta0=500;
tetaL=300;
L=0.15;
a=6.647e-6;
n=3;
t0=0;
tf=4000;
temp0=10;
temp_init=temp0*ones(1,n);
%parametri de metoda
dx=L/(n+1);
dt=10;
t=t0:dt:tf;
m=length(t);
alfa=2*dx^2/(a*dt);
%matricea temperaturilor
tempL=[teta0 temp_init tetaL];
tempCN=tempL;
% metoda implicita Crank-Nicholson
ACN=(2+alfa)*eye(n);
for j=1:(n-1)
ACN(j,j+1)=-1;
ACN(j+1,j)=-1;
end;
dCN=det(ACN);
for i=1:(m-1)
tempCN(i+1,1)=teta0;
tempCN(i+1,n+2)=tetaL;
bCN(1)=(alfa-2)*tempCN(i,2)+tempCN(i,3)+2*teta0;
bCN(n)=(alfa-2)*tempCN(i,n+1)+tempCN(i,n)+2*tetaL;
for j=2:(n-1)
bCN(j)=tempCN(i,j)-(2-alfa)*tempCN(i,j+1)+tempCN(i,j+2);
end;
bCN=bCN';
mat_aux=ACN;
for j=2:(n+1)
mat_aux(:,j-1)=bCN;
tempCN(i+1,j)=det(mat_aux)/dCN;
mat_aux=ACN;
end;
clear bCN;
end;
% metoda implicita Laasonen

AL=(2+alfa/2)*eye(n);
for j=1:(n-1)
AL(j,j+1)=-1;
AL(j+1,j)=-1;
end;
dL=det(AL);

for i=1:(m-1)
tempL(i+1,1)=teta0;
tempL(i+1,n+2)=tetaL;
bL=alfa/2*tempL(i,2:n+1)'+[teta0 zeros(1,n-2) tetaL]';
mat_aux=AL;

for j=2:(n+1)
mat_aux(:,j-1)=bL;
tempL(i+1,j)=det(mat_aux)/dL;
mat_aux=AL;
end;
end;
% reprezentari grafice
subplot(211);
plot(t,tempCN,'k');
grid;
xlabel('timp[s]');
ylabel('temperatura[grade celsius]');
title('Evolutia temperaturii--metoda Crank Nicholson');
subplot(212);
plot(t,tempL,'r');
grid;
xlabel('timp[s]');
ylabel('temperatura[grade celsius]');
title('Evolutia temperaturii--metoda Laasonen');
pause;
close(gcf);
26. metoda lui Newton (metoda tangentei)de determinare a solutiei unei ecuatii algebrice
sau transcendente
function [x,nrit]=met_tang(nume_f,nume_fd,x0,prec)
% pt aplicatie folosim functiile f1(x)=x^2-7*x+6 si f2(x)=exp(x)-5*x
x1=x0+2*prec;
nrit=0;
while abs(x1-x0)>prec
x1=x0;
x0=x1-feval(nume_f,x1)/feval(nume_fd,x1);
nrit=nrit+1;
end;
x=x0;
27. calculul de integrale definite cu ajutorul metodei trapezelor
% a se urmari functia pollagr unde se gaseste un apel pt a afla aria intre doua abscise
de sub curba de interpolare
function I=met_trpz(nume_f,a,b)
n=500;
h=abs(b-a)/(n-1);
I=0;
for k=1:n-1
x1=a+(k-1)*h;
x2=x1+h;
I=I+h/2*(feval(nume_f,x1)+feval(nume_f,x2));
end;
28. metoda Newton Raphson de rezolvare a sistemelor patratice de ec neliniare
function[sol,iter]=newtraph(nume_F,nume_dF,x0,prec)
n=length(x0);
dx=2*prec*ones(n,1);
x_c=x0;
iter=0;
while (norm(dx)>prec)
J=feval(nume_dF,x_c);
dx=-inv(J)*feval(nume_F,x_c);
x_c=x_c+dx;
iter=iter+1;
end;
sol=x_c;
% pt rezolvarea unui sistem dat mai folosim doua functii si anume y=F(x) si J=dF(x)a se
cauta in directorul curent pt un exemplu dat
%pt rezolvarea unui alt sistem se impune schmbarea coeficientilor pt n=1,2 ,3 ,4... pt
y(1),y(2),y(3)....din F(x) si coeficientii iacobianului dF(x)
29. metode de optimizare parametrica
% metode indirecte:metoda de gradient(Cauchy)
%functie de minimizat.
function[x,nr_it]=opt_grad(x0,epsilon)
nr_it=1;
x_curent=x0;
itermax=1500;
h=0.001;
p=0.001;
n=length(x0);
for k=1:n
v=zeros(1,n);
v(k)=1;
grad_curent(k)=(f3(x0+h/2*v)-f3(x0-h/2*v))/h;
end;
while (norm(grad_curent)>=epsilon)&(nr_it<=itermax)
%pt o funtie de maximizat se inlocuieste - cu +
x_viitor=x_curent-p*grad_curent/norm(grad_curent);
for k=1:n
v=zeros(1,n);
v(k)=1;
grad_viitor(k)=(f3(x_viitor+h/2*v)-f3(x_viitor-h/2*v))/h;
end;
x_curent=x_viitor;
grad_curent=grad_viitor;
nr_it=nr_it+1;
end;
x=x_curent;
30. metode directe de optimizare parametrica.metode de relaxare
%metoda relaxarii
function[x_opt,nr_it]=opt_rlx(fct_scop,x0,pas_init,r,tol)
% initializari
i=1;
x(i,:)=x0;
p=pas_init;
n=length(x0);
while(p>tol)
p0=p*r;
p=p0;
k=1;
d=zeros(1,n);
stop_global=0;
inapoi=0;
while(~stop_global)
d(k)=1;
j=0;
stop=0;
while(~stop)
if inapoi
p=p/2;
inapoi=0;
end;
x(i+1,:)=x(i,:)+p*d;
j=j+1;
if feval(fct_scop,x(i+1,:))>feval(fct_scop,x(i,:))
if j==1
p=-2*p;
i=i+1;
x(i+1,:)=x(i,:)+p*d;
inapoi=1;
else stop=1;
x(i+1,:)=[];
i=i-1;
d(k)=0;
k=k+1;
p=p0;
end;
end;
i=i+1;
end;
if k<=n
stop_global=0;
else stop_global=1;
end;
end;
end;
[nr_it n]=size(x);
x_opt=x(nr_it,:);
% pt exemplificare folosim o functie de doua var reale f4(x)
% un exemplu de apel din linia de comanda este: >> [x_opt,nr_it]=opt_rlx('f4',[-1
3],1,0.5,1e-6)
31. functie pt aflarea raspunsului unui sistem de ordin 1 la intrare treapta
%folosind metoda Runge-Kutta
function yd=ord1(t,y);
global kp u tp;
yd=-y/tp+kp*u/tp;
end;
32. programul pt simularea raspunsului unui sistem de ordin 1 la intrare treapta prin
metoda multipas Adams-Bashfort III si amorsarea cu Runge Kutta II
global kp tp u;
t=t0;y=y0;
tc=t0;yc4=y0;
k0=h*ord1(tc,yc4);
k1=h*ord1(tc,yc4+k0);
yc3=yc4+(k0+k1)/2;
tc=tc+h;
k0=h*ord1(tc,yc3);
k1=h*ord1(tc+h,yc3+k0);
yc2=yc3+(k0+k1)/2;
tc=tc+h;
k0=h*ord1(tc,yc3);
k1=h*ord1(tc+h,yc2+k0);
yc1=yc2+(k0+k1)/2;
tc=tc+h;
for i=4:q/h
yc=yc1+h*(55*ord1(tc,yc1)-59*ord1(tc-h,yc2)+37*ord1(tc-2*h,yc3)-9*ord1(tc-
3*h,yc4))/24;
tc=tc+h;
yc4=yc3;yc3=yc2;yc2=yc1;yc1=yc;
end;
t(2)=tc;y(2)=yc;

for k=3:(tf-t0)/q+1
for i=1:q/h
yc=yc1+h*(55*ord1(tc,yc1)-59*ord1(tc-h,yc2)+37*ord1(tc-2*h,yc3)-9*ord1(tc-
3*h,yc4))/24;
tc=tc+h;
yc4=yc3;yc3=yc2;yc2=yc1;yc1=yc;
end;
t(k)=tc;
y(k)=yc;
end;
clf;
plot(t,y,'-r');
title('raspunsul sistemului de ord 1 cu metoda AB 3 cu amorsare RK 2');
grid;
33. program pt simularea raspunsului sistemului de ord1 la intrare sinusoidala cu metoda
Adams_Moulton III si amorsare si predictie Runge_KuttaII
global kp tp a omega;
t(1)=t0;u(1)=a*sin(omega*t0);
y=y0;yc3=y0;
k0=h*ord1sign_sin(tc,yc3);
k1=h*ord1sign_sin(tc+h/2,yc3+k0/2);
k2=h*ord1sign_sin(tc+h,yc3+2*k1-k0);
yc2=yc3+(k0+4*k1+k2)/6;
tc=tc+h;
k0=h*ord1sign_sin(tc,yc2);
k1=h*ord1sign_sin(tc+h/2,yc2+k0/2);
k2=h*ord1sign_sin(tc+h,yc2+2*k1-k0);
yc1=yc2+(k0+4*k1+k2)/6;
tc=tc+h;
for i=3:q/h
k0=h*ord1sign_sin(tc,yc1);
k1=h*ord1sign_sin(tc+h/2,yc1+k0/2);
k2=h*ord1sign_sin(tc+h,yc1+2*k1-k0);
yc1=yc2+(k0+4*k1+k2)/6;
yc=yc1+h*(9*ord1sign_sin(tc+h,yc)+19*ord1sign_sin(tc,yc1)-5*ord1sign_sin(tc-
h,yc2)+ord1sign_sin(tc-2*h,yc3))/24;
yc3=yc2;yc2=yc1;yc1=yc;
tc=tc+h;
end;
t(2)=tc;
y(2)=yc;
u(2)=a*sin(omega*t(2));
for k=4:(tf-t0)/q+1
for i=1:q/h
k0=h*ord1sign_sin(tc,yc1);
k1=h*ord1sign_sin(tc+h/2,yc1+k0/2);
k2=h*ord1sign_sin(tc+h,yc1+2*k1-k0);
yc1=yc2+(k0+4*k1+k2)/6;
yc=yc1+h*(9*ord1sign_sin(tc+h,yc)+19*ord1sign_sin(tc,yc1)-5*ord1sign_sin(tc-
h,yc2)+ord1sign_sin(tc-2*h,yc3))/24;
yc3=yc2;yc2=yc1;yc1=yc;
tc=tc+h;
end;
t(k)=tc;
y(k)=yc;
tc=tc+h;
end;
clf;
subplot(211);
plot(t,y,'-r');
title('raspunsul sistemului');
grid;
subplot(212);
plot(t,u,'-b');
title('intrarea sistemului');
grid;
34. simularea raspunsului sistemului de ord 1 cu ODE23 la in: sinusoidala
echo off;
global kp tp a omega;
kp=1;tp=2;a=2;omega=3;t0=0;tf=10;
y0=0;
[t,y]=ODE23('ord1sign_sin',[t0 tf],y0);
for k=1:length(t)
u(k)=a*sin(omega*t(k));
end;
clf;
subplot(211)
plot(t,y,'-r');
grid;
subplot(212) plot(t,u,'-b'); grid;
35. programul principal pt aflarea raspunsului unui sistem de ordin 1 la intrare treapta
%folosind metoda Runge-Kutta I
echo off;
global kp tp u;
t(1)=t0;
y(1)=y0;
tc=t0;%valoare curenta a timpului in punctul de integrare curent
yc=y0;%valoarea calculata in punctul tc sau tc+h
for k=2:(tf+t0)/q+1
for i=1:q/h
yc=yc + ord1(tc,yc);
tc =tc+h;
end;
t(k)=tc;
y(k)=yc;
end;
clf;
plot(t,y,'-r');
title('raspunsul sistemului de ordin 1 la intrare treapta');
xlabel('t[sec]');
grid;
36. programul pt simularea raspunsului unui sistem de ordin 1 la intrare treapta cu
metoda unipas Runge Kutta III ambele variante
global kp tp u;
t=t0;ya=y0;yb=y0;
tc=t0;yca=y0;ycb=y0;
for k=2:(tf-t0)/q+1
for i=2:q/h
k0=h*ord1(tc,yca);
k1=h*ord1(tc+h/2,yca+k0/2);
k2=h*ord1(tc+h,yca+2*k1-k0);
yca=yca+(k0+4*k1+k2)/6;
k0=h*ord1(tc,ycb);
k1=h*ord1(tc+h/3,ycb+k0/3);
k2=h*ord1(tc+2*h/3,ycb+2*k1/3);
ycb=ycb+(k0+3*k2)/4;
tc=tc+h;
end;
t(k)=tc;
ya(k)=yca;
yb(k)=ycb;
end;
clf;
figure(1)
plot(t,ya,'-r');
title('raspunsul sistemului de ord 1 cu metoda RK 3 varianta a');
figure(2)
plot(t,yb,'-b');
title('raspunsul sistemului de ord 1 cu metoda RK3 varianta b');
37. programul principal pt aflarea raspunsului unui sistem de ord 1 la intrare
sinusoidala cu amplitudine si pulsatie date
%folosind metoda Runge-Kutta I
global kp tp a omega;
t(1)=t0;
y(1)=a*sin(omega*t0);
u(1)=a*sin(omega*t0);
tc=t0;
yc=a*sin(omega*t0);
for k=2:(tf-t0)/q+1
for i=1:q/h
yc=yc+h*ord1sign_sin(tc,yc);
tc=tc+h;
end;
t(k)=tc;
y(k)=yc;
u(k)=a*sin(omega*tc);
end
clf;
plot(t,y,'-r',t,u,'-b');
title('raspunsul sistemului de ordin 1 la intrare semnal sinusoidal');
xlabel('t[sec]');
grid;
38. functia pt aflarea raspunsului sistemului de ordin 1 la intrare sinusoidala cu
amplitudine si pulsatie date
function yd=ord1sign_sin(t,y);
global kp tp a omega;
yd=-y/tp+kp*a*sin(omega*t)/tp;
39. program pt simularea raspunsului unui sitem de ord 1 la intrare sinusoidala prin
metoda PCIII(predictie Adams-Bashforth3 si corectie Adams-Moulton3)
global kp tp a omega;
t(1)=t0;tc=t0;yc4=y0;y(1)=y0;
u(1)=a*sin(omega*t0);
yc3=yc4+h*ord1sign_sin1(tc,yc4);
tc=tc+h;
yc2=yc3+h*ord1sign_sin1(tc,yc3);
tc=tc+h;
yc1=yc2+h*ord1sign_sin1(tc,yc2);
tc=tc+h;
for i=4:q/h
yca=yc+h*(5*ord1sign_sin1(tc,yc1)-59*ord1sign_sin1(tc-h,yc2)+37*ord1sign_sin1(tc-
2*h,yc3)-9*ord1sign_sin1(tc-3*h,yc4))/24;
yc=yc1+h*(9*ord1sign_sin1(tc+h,yca)+19*ord1sign_sin1(tc,yc1)-5*ord1sign_sin1(tc-h,yc)
+ord1sign_sin1(tc-2*h,yc3))/24;
while abs(yc-yca)>epsimpus
yca=yc;
yc=yc1+h*(9*ord1sign_sin1(tc+h,yca)+19*ord1sign_sin1(tc,yc1)-5*ord1sign_sin1(tc-
h,yc)+ord1sign_sin1(tc-2*h,yc3))/24;
end;
yc4=yc3;yc3=yc2;yc2=yc1;yc1=yc;tc=tc+h;
end;
t(2)=tc;yc(2)=yc;u(2)=a*sin(omega*t(2));
for k=3:(tf-t0)/q+1
for i=1:q/h
yca=yc1+h*(5*ord1sign_sin1(tc,yc1)-59*ord1sign_sin1(tc-
h,yc2)+37*ord1sign_sin1(tc-2*h,yc3)-9*ord1sign_sin1(tc-3*h,yc4))/24;
yc=yc1+h*(9*ord1sign_sin1(tc+h,yca)+19*ord1sign_sin1(tc,yc1)-
5*ord1sign_sin1(tc-h,yc)+ord1sign_sin1(tc-2*h,yc3))/24;
while abs(yc-yca)>epsimpus
yca=yc;
yc=yc1+h*(9*ord1sign_sin1(tc+h,yca)+19*ord1sign_sin1(tc,yc1)-5*ord1sign_sin1(tc-
h,yc)+ord1sign_sin1(tc-2*h,yc3))/24;
end;
yc4=yc3;yc3=yc2;yc2=yc1;yc1=yc;tc=tc+h;
end;
t(k)=tc;
y(k)=yc;
u(k)=a*sin(omega*tc);
end;
clf;
subplot(211)
plot(t,y,'-b');
title('raspunsul sist ord 1 IN sin cu PC III');
grid;
subplot(212)
plot(t,u,'-y');
title('intrarea sistemului');
grid;
40. functie pt simularea rasp sist ord 2
function yd=ord2(t,y);
global kp tp zeta u;
yd(1)=y(2);
yd(2)=-2*zeta*y(2)/tp-(y(1)-kp*u)/tp^2;
41. programul pt simularea raspunsului unui sistem de ordin 2 la intrare treapta prin
metoda multipas Adams-Bashfort I cu amorsare Runge Kutta I
global kp tp u zeta;
t=t0;y=y10;y=y20;
tc=t0;yc2=[y10 y20];
yc1=yc2+h*ord2(tc,yc2);
tc=tc+h;
for i=2:q/h
yc=yc1+h*(3*ord2(tc,yc1)-ord2(tc-h,yc2));
tc=tc+h;
yc2=yc1;yc1=yc;
end;
t(2)=tc;y(2,1)=yc(1);y(2,2)=yc(2);
for k=3:(tf-t0)/q+1
for i=1:q/h
yc=yc1+h*(3*ord2(tc,yc1)-ord2(tc-h,yc2));
tc=tc+h;
yc2=yc1;yc1=yc;
end;
t(k)=tc;
y(k,1)=yc(1);
y(k,2)=yc(2);
end;
clf;
subplot(211)
plot(t,y(:,1),'-r');
title('raspunsul sistemului de ord 2 cu metoda AB 1 cu amorsare RK 1 ');
grid;
subplot(212)
plot(t,y(:,2),'-b');
title('derivata raspuns sistem de ord 2 cu metoda AB 1 cu amorsare RK1 ');
grid;
42. functie pt simularea rasp sist ord 2
function yd=ord2(t,y);
global kp tp zeta u;
yd(1)=y(2);
yd(2)=-2*zeta*y(2)/tp-(z(1)-kp*u)/tp^2;
43. functie pt rasp sistem de ord 2 neliniar la intare treapta
function yd=ord2nelin(t,y);
global kp tp omega u;
yd(1)=(-tp^2)*y(1)/(1+abs(tp))+y(2);
yd(2)=(sin(omega*tp)*u-sqrt(1+t)*y(1))/(1+abs(tp));
44. rasp sist ord 2 la intrare trapta prin metoda Runge-Kutta IV
global kp tp omega u;
y=y1;
y=y2;
tc=t0;
yc=[y1 y2];
y=y0;
for k=2:(tf-t0)/q+1
for i=1:q/h
k0=h*ord2nelin(tc,yc);
k1=h*ord2nelin(tc+h/2,yc+k0/2);
k2=h*ord2nelin(tc+h/2,yc+k1/2);
k3=h*ord2nelin(tc+h,yc+k2);
yc=yc+(k0+2*k1+2*k2+k3)/6;
tc=tc+h;
end;
t(k)=tc;
y(k,1)=yc(1);
y(k,2)=yc(2);
end;
clf;
figure(1)
plot(t,y(:,2),'-b');
title('raspuns sistem ord 2 RK 4 la intrare treapta ');
grid;
45. functie pt simularea rasp sist ord 2 imbunatatit pt ODE
function yd=ord2(t,y);
global kp tp zeta u;
yd(1)=y(2);
yd(2)=-2*zeta*y(2)/tp-(y(1)-kp*u)/tp^2;
yd=yd';
46. simularea raspunsului sistemului de ord 2 cu ODE45 la in: treapta
echo off;
global kp tp zeta u;
kp=1;tp=2;t0=0;tf=50;
y10=0;u=1;
y20=0; zeta=0.2;
[t,y]=ODE45('ord2ODE',[t0 tf],[y10 y20]);
for k=1:length(t)
suma(k)=y(k,1)+y(k,2);
end;
clf;
subplot(211)
plot(t,y(:,1),'-r');
title('raspuns');
grid;
subplot(212)
plot(t,suma,'-b');
title('suma');
grid;
47. program de simulare a rasp a sistemului de ord 2kp/tp^1*s^2+2*zeta*tp*s+1 pentru IN
treapta prin metoda PC II
global kp tp zeta u;
t(1)=t0;y(1,1)=y10;y(2,1)=y20;
yc3=[y10 y20];
tc=t0;
k0=h*ord2(tc,yc3);
k1=h*ord2(tc+h/2,yc3+k0/2);
k2=h*ord2(tc+h,yc3+2*k1-k0);
yc2=yc3+(k0+4*k1+k2)/6;
tc=tc+h;
k0=h*ord2(tc,yc2);
k1=h*ord2(tc+h/2,yc2+k0/2);
k2=h*ord2(tc+h,yc2+2*k1-k0);
yc1=yc2+(k0+4*k1+k2)/6;
tc=tc+h;
for i=3:q/h
yca=yc1+h*(23*ord2(tc,yc1)-16*ord2(tc-h,yc2)+5*ord2(tc-2*h,yc3))/12;
yc=yc1+h*(5*ord2(tc+h,yca)+8*ord2(tc,yc1)-ord2(tc-h,yc2))/12;
while sqrt((yc(1)-yca(1))^2+(yc(2)-yca(2))^2)>epsimpus
yca=yc;
yc=yc1+h*(5*ord2(tc+h,yca)+8*ord2(tc,yc1)-ord2(tc-h,yc2))/12;
end;
tc=tc+h;
yc3=yc2;yc2=yc1;yc1=yc;
end;
t(2)=tc;y(2,1)=yc(1);y(2,2)=yc(2);
for k=3:(tf-t0)/q+1
for i=1:q/h
yca=yc1+h*(23*ord2(tc,yc1)-16*ord2(tc-h,yc2)+5*ord2(tc-2*h,yc3))/12;
yc=yc1+h*(5*ord2(tc+h,yca)+8*ord2(tc,yc1)-ord2(tc-h,yc2))/12;
while sqrt((yc(1)-yca(1))^2+(yc(2)-yca(2))^2)>epsimpus
yca=yc;
yc=yc1+h*(5*ord2(tc+h,yca)+8*ord2(tc,yc1)-ord2(tc-h,yc2))/12;
end;
tc=tc+h;
yc3=yc2;yc2=yc1;yc1=yc;
end;
t(k)=tc;
y(k,1)=yc(1);
y(k,2)=yc(2);
end;
clf;
plot(t,y(:,1),'-r');
title('raspunsul sistemului');
grid;
48. program principal pt aflarea raspunsului unui sistem de ordin 2 cu 2 poli si 1 zerou
la intrare treapta
%folosind metoda Runge-Kutta I
global kp tp zeta u;
t(1)=t0;
tc=t0;yc2=y0;
y2(1,1)=y20;y2(1,2)=y10;
for k=2:(tf-t0)/q+1
for i=1:q/h
yc2=yc2+h*ord22p1z(tc,yc2);
tc=tc+h;
end
t(k)=tc;
y2(k,1)=yc2(1);
y2(k,2)=yc2(2);
end;
clf;
plot(t,z(:,2),'-r');
title('raspunsul sitemului de ordin 2 la intrare treapta cu 2 poli si 1 zerou');
grid;
49. programul pt simularea raspunsului unui sistem de ordin 2 la intrare treapta cu
metoda unipas Runge Kutta IV
global kp tp u zeta;
t(1)=t0;y(1,1)=y10;y(1,2)=y20;
tc=t0;yc=[y10 y20];
for k=2:(tf-t0)/q+1
for i=2:q/h
k0=h*ord2(tc,yc);
k1=h*ord2(tc+h/2,yc+k0/2);
k2=h*ord2(tc+h/2,yc+k1/2);
k3=h*ord2(tc+h,yc+k2);
yc=yc+(k0+2*k1+2*k2+k3)/6;
tc=tc+h;
end;
t(k)=tc;
y(k,1)=yc(1);
y(k,2)=yc(2);
end;
clf;
subplot(211)
plot(t,y(:,1),'-r');
title('raspunsul sistemului de ord 2 cu metoda RK 4 ');
subplot(212)
plot(t,y(:,2),'-b');
title('derivata raspuns sistem de ord 2 cu metoda RK4 ');
50. program principal pt aflarea raspunsului unui sistem de ordin 2 la intrare treapta
%folosind metoda Runge-Kutta I pt AMANDOUA SCHIMBARI DE VARIABILA la forma standard
echo off;
global kp tp zeta u;
t(1)=t0;y1(1,1)=y10;
y1(1,2)=y20;tc=t0;yc1=[y10 y20];yc2=[y20 y10];
y2(1,1)=y20;y2(1,2)=y10;
for k=2:(tf-t0)/q+1
for i=1:q/h
yc1=yc1+h*ord2schimbvar1(tc,yc1);
yc2=yc2+h*ord2schimbvar2(tc,yc2);
tc=tc+h;
end
t(k)=tc;
y1(k,1)=yc1(1);
y1(k,2)=yc1(2);
y2(k,1)=yc2(1);
y2(k,2)=yc2(2);
end;
clf;
figure (1) % pt prima schimbare de variabila;
subplot(211);
plot(t,y1(:,1),'-m');
title('raspunsul sistemului de ordin 2 la intrare treapta-prima schimb. de var');
grid;
xlabel('t[sec]');
subplot(212);
plot(t,y1(:,2),'*g');
title('derivata raspuns sistemului ordin 2');
xlabel('t[sec]');
grid;
figure (2) % pt a doua schimbare de variabila;
subplot(211);
plot(t,y2(:,2),'-r');
title('raspunsul sitemului de ordin 2 la intrare treapta cu a IIa schimb de var');
grid;
subplot(212);
plot(t,y2(:,1),'*b');
title('expresie cu raspunsul sitemului de ordin 2');
grid;
figure(3)
plot(t,y1(:,1),'-b',t,y2(:,2),'-g');l
title('raspunsurile sist ord 2 dupa cele2 schimb de var');
grid;
51. functie pt aflarea raspunsului unui sistem de ordin 2 la intrare treapta
%folosind metoda Runge-Kutta I pt PRIMA SCHIMBARE DE VARIABILA la forma standard
function yd=ord2schimbvar1(t,y);
global kp tp zeta u;
yd(1)=y(2);
yd(2)=-y(1)/tp^2-2*zeta*y(2)/tp+kp*u/tp^2;
end;
52. functie pt aflarea raspunsului unui sistem de ordin 2 la intrare treapta
%folosind metoda Runge-Kutta I pt A DOUA SCHIMBARE DE VARIABILA la forma standard
function yd=ord2schimbvar2(t,y);
global kp tp zeta u;
yd(1)=kp*u/tp^2-y(2)/tp^2;
yd(2)=-2*zeta*y(2)/tp+y(1);
end;
53. functie pt calcul rasp sistem ord 2 cu 2 poli si 0 zerouri
function yd=ord22p0z(t,y)
global kp t1 t2 u;
yd(1)=(-(t1+t2)*y(1))/(t1*t2)+y(2);
yd(2)=(kp*u-y(1))/t1*t2;
54. program pt simularea raspunsului sistemului de ord1 la intrare treapta cu metoda
Adams_Moulton I si amorsare si predictie Runge_KuttaIV
global kp t1 t2 u;
t(1)=t0;
y(1,1)=y10;y(1,2)=y20;
tc=t0;
k0=h*ord22p0z(tc,yc1);
k1=h*ord22p0z(tc+h/2,yc1+k0/2);
k2=h*ord22p0z(tc+h/2,yc1+k1/2);
k3=h*ord22p0z(tc+h,yc1+k2);
yc=yc1+(k0+2*k1+2*k2+k3)/6;
tc=tc+h;
for i=2:q/h
k0=h*ord22p0z(tc,yc1);
k1=h*ord22p0z(tc+h/2,yc1+k0/2);
k2=h*ord22p0z(tc+h/2,yc1+k1/2);
k3=h*ord22p0z(tc+h,yc1+k2);
yc=yc1+(k0+2*k1+2*k2+k3)/6;
yc=yc1+h*(ord22p0z(tc+h,yc)+ord22p0z(tc,yc1))/2;
yc1=yc;
tc=tc+h;
end;
t(2)=tc;
y(2,1)=yc(1);
y(2,2)=yc(2);
for k=3:(tf-t0)/q+1
for i=1:q/h
k0=h*ord22p0z(tc,yc1);
k1=h*ord22p0z(tc+h/2,yc1+k0/2);
k2=h*ord22p0z(tc+h/2,yc1+k1/2);
k3=h*ord22p0z(tc+h,yc1+k2);
yc=yc1+(k0+2*k1+2*k2+k3)/6;
yc=yc1+h*(ord22p0z(tc+h,yc)+ord22p0z(tc,yc1))/2;
yc1=yc;
tc=tc+h;
end;
t(k)=tc;
y(k,1)=yc(1);
y(k,2)=yc(2);
end;
clf;
plot(t,y(:,2),'-r');
title('raspunsul sistemului');
grid;
55. functie pt determinarea raspunsului unui sistem de ordin 2 cu 2poli si 1 zerou la
intrare treapta
function yd=ord22p1z(t,y);
global kp t1 t2 t3 u;
yd(1)=(kp*t1*u-(t2+t3)*y(1))/(t2*t3)+y(2);
yd(2)=(kp*u-y(1))/(t2*t3);

%factorizarea QR unei matrice A din R(mxn),m>n,rg A=n(A matrice monica)

a1=a;

q=eye(m);
for k=1:n
sig=0;
for i=k:m
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);

for i=1:m
if i<=k-1
v(i)=0;
elseif i==k
v(i)=a(k,k)+sig;
else v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(m)-(v'*v)/beta;
a=q1*a;
q=q*q1;

end;

%verificarea o facem in linia de comanda pt matricea a=[2 4 6;8 9 7;5 0 2] se verifica


q*a=a1 si inv(q)=q'
% factorizarea QR pt omatrice A din R(mxn) cu rg A<min(m,n)

a1=a;
q=eye(m);
p=eye(n);
k=1;
ind=1;
for j=1:n
norm(j)=0;
for i=1:m
norm(j)=norm(j)+a(i,j)^2;
end;
end;
while ind==1&k<=n
l=k;
for j=k+1:n
if norm(j)>norm(l)
l=j;
end;
end;
if norm(l)==0
ind=0;
else
if l~=k
p1=eye(n);
p1(k,l)=1;
p1(l,k)=1;
p1(k,k)=0;
p1(l,l)=0;
a=a*p1;
p=p*p1;
end;
sig=0;
for i=k:m
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);
for i=1:m
if i<=k-1
v(i)=0;
elseif i==k
v(i)=sig+a(k,k);
else v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(m)-(v'*v)/beta;
a=q1*a;
q=q*q1;
k=k+1;
for j=k:n
norm(j)=0;
for i=1:m
norm(j)=norm(j)+a(i,j)^2;
end;
end;
end;
end;
r=0;
while a(r+1,r+1)~=0
r=r+1;
end;

% verificarea q*a=a1*q si inv(q)=q' , inv(p)=p'


% program pt a aduce "in situ" perechea de matrice(A,B) A,B din R(nxn) la forma
Hessenberg generalizata

a1=a;
b1=b;
q=eye(n);
z=eye(n);

%etapa 1

for k=1:n-1
sig=0;
for i=k:n

sig=sig+b(i,k)^2;
end;
sig=sign(b(k,k))*sqrt(sig);
for i=1:n
if i<=k-1
v(i)=0;
elseif i==k
v(i)=sig+b(k,k);
else v(i)=b(i,k);
end;
end;
beta=sig*(sig+b(k,k));
q1=eye(n)-(v'*v)/beta;
a=q1*a;
b=q1*b;
q=q1*q;
end;

%etapa 2

for k=1:n-2
for i=n:-1:k+2
sig=sign(a(i-1,k))*sqrt(a(i,k)^2+a(i-1,k)^2);
for j=1:n
if j<=i-2
v(j)=0;
elseif j==i-1
v(j)=sig+a(i-1,k);
else v(j)=a(j,k);
end;
end;
beta=sig*(sig+a(i-1,k));
q1=eye(n)-(v'*v)/beta;
a=q1*a;
b=q1*b;
q=q1*q;

sig=sign(b(i,i))*sqrt(b(i,i)^2+b(i,i-1)^2);

for j=1:n
if j<=i-2
v(j)=0;
elseif j==i-1
v(j)=b(i,i-1);
elseif j==i
v(j)=sig+b(i,i);
else v(j)=0;
end;
end;
beta=sig*(sig+b(i,i));
z1=eye(n)-(v'*v)/beta;

a=a*z1;
b=b*z1;
z=z*z1;
end;
end;

%implicatii ale factorizarii QR


%calculul pseudoinversei
%program de calcul pt inversa unei matrice U din R(nxn) superior triunghiulara "in-situ"

u1=u;
for k=n:-1:1
u(k,k)=1/u(k,k);
for i=k-1:-1:1
s=0;
for j=i+1:k
s=s+u(i,j)*u(j,k);
end;
u(i,k)=-s/u(i,i);
end;
end;

% verificarea u*u1=I

%factorizarea unei matrice A din R(mxn),m>n,rg A=n(A matrice monica)

a1=a;

q=eye(m);
for k=1:n
sig=0;
for i=k:m
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);

for i=1:m
if i<=k-1
v(i)=0;
elseif i==k
v(i)=a(k,k)+sig;
else v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(m)-(v'*v)/beta;
a=q1*a;
q=q*q1;

end;

%verificarea o facem in linia de comanda pt matricea a=[2 4 6;8 9 7;5 0 2] se verifica


a*q=a1 si inv(q)=q'
% programul de factorizare QR(cu acumularea transformarilor) a unei matrice A din R(nxn)
nesingulara

a1=a;

q=eye(n);
for k=1:n-1
sig=0;
for i=k:n
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);

for i=1:n
if i<=k-1
v(i)=0;
elseif i==k
v(i)=a(k,k)+sig;
else v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(n)-(v'*v)/beta;
a=q1*a;
q=q*q1;

end;

% pt verificare in linia de comanda se introduce >>q*a si trebuie sa returneze a1 si


>>inv(q) trbuie sa fie egal cu >>q'

% program de rezolvare a sistemelor Ax=b cu a din R(mxn) si m>n,rang A=n(A monica)

a1=a;
b1=b;
for k=1:n
sig=0;
for i=k:m
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);
for i=1:m
if i<=k-1
v(i)=0;
elseif i==k;
v(i)=sig+a(k,k);
else
v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(m)-(v'*v)/beta;
a=q1*a;
b=q1*b;
end;
b(n)=b(n)/a(n,n);
for i=n-1:-1:1
s=0;
for j=i+1:n
s=s+a(i,j)*b(j);
end;
b(i)=(b(i)-s)/a(i,i);
end;

%program de rezolvare a sistemelor de ecuatii Ax=b cu A din R(nxn)-nesingulara cu


ajutorul factorizarii QR

a1=a;
b1=b;
for k=1:n-1
sig=0;
for i=k:n
sig=sig+a(i,k)^2;
end;
sig=sign(a(k,k))*sqrt(sig);
for i=1:n
if i<=k-1
v(i)=0;
elseif i==k;
v(i)=sig+a(k,k);
else
v(i)=a(i,k);
end;
end;
beta=sig*(sig+a(k,k));
q1=eye(n)-(v'*v)/beta;
a=q1*a;
b=b*q1;
end;
b(n)=b(n)/a(n,n);
for i=n-1:-1:1
s=0;
for j=i+1:n
s=s+a(i,j)*b(j);
end;
b(i)=(b(i)-s)/a(i,i);
end;

S-ar putea să vă placă și