0% found this document useful (0 votes)
47 views12 pages

Notite Aplicatii-Matlab Master 2023

The document contains examples of using MATLAB for: - Solving nonlinear equations using fsolve and defining an anonymous function - 1D and 2D interpolation of data using interp1 and interp2 - Linear regression using polyfit to find coefficients of polynomials fitting data up to order 4 - Computing R-squared values to evaluate polynomial fit models

Uploaded by

Andrea Andreolli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views12 pages

Notite Aplicatii-Matlab Master 2023

The document contains examples of using MATLAB for: - Solving nonlinear equations using fsolve and defining an anonymous function - 1D and 2D interpolation of data using interp1 and interp2 - Linear regression using polyfit to find coefficients of polynomials fitting data up to order 4 - Computing R-squared values to evaluate polynomial fit models

Uploaded by

Andrea Andreolli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LABORATOR Nr.

Rezolvare:

>> x=4.2;
>> h=(2*x+5-[43.9/2.5*x-x.^3*(3*pi/4-7)])/(2*x+5)
h=
-30.1793
>> format rat
>> disp('h=');disp(h)
h=
-3199/106

Rezolvare:

>> A=[1 5 7.5;75 81 9;12 3*pi 8.3];


>> disp('A=');disp(A)
A=
1 5 15/2
75 81 9
12 1065/113 83/10
>> format short
>> A=[1 5 7.5;75 81 9;12 3*pi 8.3];
>> disp('A=');disp(A)
A=
1.0000 5.0000 7.5000
75.0000 81.0000 9.0000
12.0000 9.4248 8.3000
>> format bank
>> disp('A=');disp(A)
A=
1.00 5.00 7.50
75.00 81.00 9.00
12.00 9.42 8.30
>> a=A(1,1)
a=
1.00
>> b=A(2,1)
b=
75.00
>> c=A(1,2)
c=
5.00
>> d=A(3,3)
d=
8.30
>> e=A(1,1)
e=
1.00
>> f=A(:,1)
f=
1.00
75.00
12.00
>> g=A(3,:)
g=
12.00 9.42 8.30
>> h=A(:,1:2)
h=
1.00 5.00
75.00 81.00
12.00 9.42
>> i=A(2:3,:)
i=
75.00 81.00 9.00
12.00 9.42 8.30

Rezolvare:

>> t=[0:20:300]
t=
Columns 1 through 8
0 20.00 40.00 60.00 80.00 100.00 120.00 140.00
Columns 9 through 16
160.00 180.00 200.00 220.00 240.00 260.00 280.00 300.00
>> t=t'
t=
0
20.00
40.00
60.00
80.00
100.00
120.00
140.00
160.00
180.00
200.00
220.00
240.00
260.00
280.00
300.00
>> t=linspace(0,300,16)
t=
Columns 1 through 8
0 20.00 40.00 60.00 80.00 100.00 120.00 140.00
Columns 9 through 16
160.00 180.00 200.00 220.00 240.00 260.00 280.00 300.00

Rezolvare:

>> A=[1 2;3 4]


A=
1.00 2.00
3.00 4.00
>> C=[5 6;7 8]
C=
5.00 6.00
7.00 8.00
>> a=A*C
a=
19.00 22.00
43.00 50.00
>> b=A.*C
b=
5.00 12.00
21.00 32.00
>> c=A.^C
c=
1.00 64.00
2187.00 65536.00

>> d=A./C
d=
0.20 0.33
0.43 0.50

Rezolvare:

>> A=[3 2 -1;4 -3 2;6 -2 5]


A=
3.00 2.00 -1.00
4.00 -3.00 2.00
6.00 -2.00 5.00
>> B=[3 -4 7]
B=
3.00 -4.00 7.00
>> B=B'
B=
3.00
-4.00
7.00
>> X=inv(A)*B
X=
-0.10
3.02
2.73
>> X=A\B
X=
-0.10
3.02
2.73

Rezolvare:

>> A=[2 1 2;42 2 3;1 3 3]


A=
2.00 1.00 2.00
42.00 2.00 3.00
1.00 3.00 3.00
>> B=[5 7 6]
B=
5.00 7.00 6.00
>> B=B'
B=
5.00
7.00
6.00
>> X=inv(A)*B
X=
-0.00
-1.00
3.00
>> t=[0 1 2 3 4 5 6 7 8 9 10 11 12];
>> y=[0 0.55 1 2 4 7.2 11 14 15.1 16 16 16 16];
>> plot(t,y,'ro-'); grid;
>> xlabel('variabila independenta t');
>> ylabel('variabila dependenta y');

Rezolvare:

>> x=-2:0.1:2;
>> y=exp(-x.^2).*cos(20*x);
>> plot(x,y,'bd--');grid;
>> xlabel('x');
>> ylabel('y');
>> title('graficul functiei y=exp(-x^2)')

Rezolvare:

>> x=0:0.02:4;
>> y=3*sin(pi*x);
>> z=exp(-0.2*x);
>> plot(x,y,'g+:',x,z,'c*-');grid;
>> xlabel('x');
>> ylabel('y');
>> title('grafic doua functii');

LABORATOR Nr.2

Rezolvare:

In zona Editor: In zona Command Window:

function F=fsysex1(X) >> X=[1 1]';


x=X(1);y=X(2); >> rezultat=fsolve('fsysex1',[1 1]')
F=zeros(2,1); rezultat =
F(1)=x^3+x*y^2-9.375;
F(2)=x*y+y^2-7;
1.50
2.00
Save as fsysex1

Rezolvare:

In zona Editor:
x=0:1.5:15;
y=[0 0.9975 0.1411 -0.9775 -0.2794 0.9380 0.4121 -0.8797 -0.5366 0.8038 0.6503];
xi=0:.25:15;
yi=interp1(x,y,xi,'linear');
plot(x,y,'bo',xi,yi,'rx-');grid;
legend('date initiale','valori rezultate prin interpolare')

Save as ……apoi Run

x=0:1.5:15;
y=[0 0.9975 0.1411 -0.9775 -0.2794 0.9380 0.4121 -0.8797 -0.5366 0.8038 0.6503];
xi=0:.25:15;
yi=interp1(x,y,xi,'spline');
plot(x,y,'bo',xi,yi,'rx-');grid;
legend('date initiale','valori rezultate prin interpolare')

Save as ……apoi Run

x=0:1.5:15;
y=[0 0.9975 0.1411 -0.9775 -0.2794 0.9380 0.4121 -0.8797 -0.5366 0.8038 0.6503];
xi=0:.25:15;
yi=interp1(x,y,xi,'pchip');
plot(x,y,'bo',xi,yi,'rx-');grid;
legend('date initiale','valori rezultate prin interpolare')

Save as ……apoi Run


In zona Command Window:

>> x=[1 2 3 4 5];


>> y=[1 2 3];
>> td=[82 81 80 82 84; 79 63 61 65 81; 84 84 82 85 86];
>> xi=1:0.2:5;
>> yi=1:0.2:3;
>> zi=interp2(x,y',td,xi,yi','cubic');
>> surf(xi,yi,zi);
>> xlabel('axa x');
>> ylabel('axa y');
>> zlabel('axa z');
>> title('graficul dependentei z=f(x,y)');
LABORATOR Nr.3

In zona Editor:

function[A,R2,stddev]=linregr1(u,y)
n=length(u); %numarul seturilor de date
B=[n sum(u);sum(u) sum(u.^2)];
C=[sum(y);sum(y.*u)];
A=B\C; %coeficientii ecuatiei de regresie
yc=A(1)+A(2)*u;
ym=sum(y)/n;
R2=(sum((yc-ym).^2)/sum((y-ym).^2)); %indicatorul de precizie a modelului
stddev=sqrt(sum((y-yc).^2)/(n-1));%deviatia standard

In zona Command Window:

>> u=0:0.05:1;
>> y=[5.05 5.2 5.55 5.7 6.05 6.22 6.58 6.7 7.05 7.21 7.55 7.8 8 8.2 8.57 8.72 9.07 9.23 9.48 9.78 9.95];
>> [A,R2,stddev]=linregr1(u,y);
>> disp('coeficientii ecuatiei de regresie, a0 si a1');disp(A');
coeficientii ecuatiei de regresie, a0 si a1
5.01 4.99
>> disp('indicatorul preciziei modelului si deviatia standard');disp([R2 stddev]);
indicatorul preciziei modelului si deviatia standard
1.00 0.05
>> Y=A(1)+A(2)*u;
>> plot(u,Y,'-',u,y,'*');
>> xlabel('variabila independenta u');
>> ylabel('variabila dependenta y');grid;
>> legend('linia de regresie', 'date experimentale');
In zona Editor:

x=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3];


y=[59.7 24.9 13.7 10.4 7.3 4.9 11.7 44.4 127.7 294.5 585.7];
p=polyfit(x,y,2);
disp('coeficientii polinomului de ordinul 2 sunt:');disp(p);
f=polyval(p,x);
plot(x,y,'ro',x,f,'k-');grid;
xlabel('variabila x');
ylabel('variabila dependenta y');
legend('valori experimentale','dependenta polinomiala de ordinul 2');
ym=mean(y);
R2=sum((f-ym).^2)/sum((y-ym).^2);
disp('indicatorul de precizie al modelului R2 este:'); disp(R2);

Run….

In zona Command Window:

>> ex2_polyfit
coeficientii polinomului de ordinul 2 sunt:
50.85 24.11 -44.17

indicatorul de precizie al modelului R2 este:


0.90

In zona Editor:

x=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3];


y=[59.7 24.9 13.7 10.4 7.3 4.9 11.7 44.4 127.7 294.5 585.7];
p=polyfit(x,y,3);
disp('coeficientii polinomului de ordinul 3 sunt:');disp(p);
f=polyval(p,x);
plot(x,y,'ro',x,f,'k-');grid;
xlabel('variabila x');
ylabel('variabila dependenta y');
legend('valori experimentale','dependenta polinomiala de ordinul 3');
ym=mean(y);
R2=sum((f-ym).^2)/sum((y-ym).^2);
disp('indicatorul de precizie al modelului R2 este:'); disp(R2);

Run….

In zona Command Window:

>> ex3_polyfit
coeficientii polinomului de ordinul 3 sunt:
16.80 25.65 -38.05 -8.89
indicatorul de precizie al modelului R2 este:
0.99

In zona Editor:

x=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3];


y=[59.7 24.9 13.7 10.4 7.3 4.9 11.7 44.4 127.7 294.5 585.7];
p=polyfit(x,y,4);
disp('coeficientii polinomului de ordinul 4 sunt:');disp(p);
f=polyval(p,x);
plot(x,y,'ro',x,f,'k-');grid;
xlabel('variabila x');
ylabel('variabila dependenta y');
legend('valori experimentale','dependenta polinomiala de ordinul 4');
ym=mean(y);
R2=sum((f-ym).^2)/sum((y-ym).^2);

Run….

In zona Command Window:

>> ex4_polyfit
coeficientii polinomului de ordinul 4 sunt:
5.40 6.00 0.00 -7.00 7.31

indicatorul de precizie al modelului R2 este:


1.00

You might also like