Anul 3 Semestrul 2

This commit is contained in:
2025-07-03 20:56:38 +03:00
parent 184f3bd92e
commit 3b7fb85767
269 changed files with 20955 additions and 0 deletions
+106
View File
@@ -0,0 +1,106 @@
A = [1,2;3,4]
det(A)
inv(A)*A
A*A
A.*A
A^2
A.^2
v=1:10
V=1:-0.1:0
v.^2
transpose(v) # or v'
A(1,:)
A(:,2)
#L1_NC.pdf
#1.a
x=-4:0.1:7.2;
p = x.^5-5*x.^4-16*x.^3+16*x.^2-17.*x+21;
#plot(x,p)
#1.b
x=-2.5;
p = [1,-5,-16,+16,-17,21];
polyval(p,x)
#1.c
roots(p)
polyval(p,7)
#2
x = 0:0.1*pi:2*pi;
f = sin(x);
g = sin(2*x);
h = sin(3*x);
#subplot(3,1,1)
#plot(x,f)
#subplot(3,1,2)
#plot(x,g)
#subplot(3,1,3)
#plot(x,h)
clf #clear plot
t= 0:0.1*pi:10*pi
R=3.8;
r=1;
x = (R+r)*cos(t) - r*cos((R/r+1)*t);
y = (R+r)*sin(t) - r*sin((R/r+1)*t);
plot(x,y)
[x, y] = meshgrid(-2:0.1:2, 0.5:0.1:4.5);
f = sin(e.^x).*cos(log(y));
clf
#mesh(x, y, f);
#xlabel('X-axis');
#ylabel('Y-axis');
#zlabel('Z-axis');
#title('3D Surface Plot using mesh');
#colormap('jet');
#colorbar;
#grid on;
figure;
plot3(x, y, f);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Line Plot using plot3');
grid on;
function result = funct(n)
if n == 0
result = 1+1;
else
result = 1 + 1/ funct(n - 1);
end
end
% Increase the recursion limit to 5000
#max_recursion_depth(2025);
funct(2)
funct(10)
funct(100)
funct(2025)
@@ -0,0 +1,9 @@
f=@(x)1./x;
R = rectangle_int(f,1,2,100)
R = trapz_int(f,1,2,100)
R = homer_int(f,1,2,100)
+25
View File
@@ -0,0 +1,25 @@
#f=@(x)((2./sqrt(pi)).*(e.^(-x.^2)));
#for b=0.1:0.1:1
# I = adquad(f,0,b,0.1,4)
# I2 = integral(f,0,b)
#end
f=@(x)(sin(x.^(1/3)));
g=@(x)(sin(x).*3.*(x.^2));
x = [2,4,8,16,32,64,128,256];
trueI = integral(f,0,1)
for n = x
n
I1=homer_int(f,0,1,n)
fprintf('%.*f \n', 10, abs(trueI-I1))
I2=homer_int(g,0,1,n)
fprintf('%.*f \n', 10, abs(trueI-I2))
end
+80
View File
@@ -0,0 +1,80 @@
#pkg load symbolic;
#1.a
syms x;
f=exp(x)
t1=taylor(f,x,0,'order',1);
t2=taylor(f,x,0,'order',2);
t3=taylor(f,x,0,'order',3);
t4=taylor(f,x,0,'order',4);
t10 = taylor(f,x,0,'order',10);
#ezplot(t1)
hold on
ezplot(t2)
hold on
ezplot(t3)
hold on
ezplot(t4)
xlim([-3,3])
#1.b
vpa(exp(1),7)
vpa(subs(t10,x,1),7)
#2.a
syms x;
f=sin(x)
t3=taylor(f,x,0,'order',3);
t5=taylor(f,x,0,'order',5);
hold off
ezplot(f)
hold on
ezplot(t3)
hold on
ezplot(t5)
xlim([-pi,pi])
ylim([-2,2])
#2.b
t10=taylor(f,x,0,'order',10);
vpa(sin(pi/5),5)
vpa(subs(t10,x,sym(pi)/5),5)
vpa(sin(10*pi/3),5)
vpa(subs(t10,x,10*sym(pi)/3),5) #it's not precise enought, increase n for Taylor series or move x0=0 to x0= 10pi/3
#3.a
syms x;
f=log(1+x);
t2= taylor(f,x,0,'order',2);
t5= taylor(f,x,0,'order',5);
hold off
ezplot(f)
hold on
ezplot(t2)
hold on
ezplot(t5)
xlim([-0.9,1])
ylim([-1,1])
t= taylor(f,x,0,'order',10);
vpa(log(2),5)
vpa(subs(t,x,1),5)
syms x;
g=log(1-x);
t2 = taylor(g,x,0,'order',10)
t-t2
vpa(subs(t,x,sym(0.999,'f')) - subs(g,x,sym(0.999,'f')),5)
+30
View File
@@ -0,0 +1,30 @@
x = [0 1 2];
f = 1./(1+x);
t=divdiff(x,f);
x = [0, 1, 2];
f = 1./(1+x);
df = (-1)./((1+x).^2);
[z,t]=divdiff2(x,f,df)
x=linspace(1,2,11);
f=1./(1+x);
df = (-1)./((1+x).^2);
t=divdiff(x,f)
[z,t]=divdiff2(x,f,df)
x = [-2, -1, 0, 1, 2, 3, 4];
f = [-5, 1, 1, 1,7 ,25, 60];
t=divdiff(x,f)
x = [-2, -1, 0, 1, 2, 3, 4];
f = [-5, 1, 1, 1,7 ,25, 60];
t=forwarddiff(f)
t=backdiff(f)
+26
View File
@@ -0,0 +1,26 @@
#U= [2,4,2;0,-1,1;0,0,-1];
#b = [8;0;-1];
#x = backsub(U,b);
A= [2 1 -1 -2; 4 4 1 3; -6 -1 10 10; -2 1 8 4];
b = [2;4;-5;1];
x = gauss(A,b);
n=5
A=5*eye(n) -diag(ones(1,n-1),1)- diag(ones(1,n-1),-1)
b = [4,3* ones(1,n-2),4]'
[L,U,P]= lu(A);
y = forwsub(L,P*b)
x = backsub(U, y)
+42
View File
@@ -0,0 +1,42 @@
n=7;
A=5*eye(n) -diag(ones(1,n-1),1)- diag(ones(1,n-1),-1);
b = [4,3* ones(1,n-2),4]';
x0 = zeros(size(b));
[x, nit] = jacobi(A,b, x0,10^(-5), 1000);
[x, nit] = gaussseidel(A,b, x0,10^(-5), 1000);
A = [10 7 8 7; 7 5 6 5; 8 6 10 9; 7 5 9 10];
b=[32;23;33;31];
x = A^-1*b
trueB = b;
trueX = x;
trueA = A;
A = [10 7 8 7; 7 5 6 5; 8 6 10 9; 7 5 9 10];
b=[32.1;22.9;33.1;30.9];
x = A^-1*b
norm(trueX-x,inf)/norm(trueX,inf)
norm(trueB-b,inf)/norm(trueB,inf)
cond(A)
A = [10 7 8.1 7.2; 7.8 5.04 6 5; 8 5.98 9.89 9; 6.99 4.99 9 9.98];
b=[32;23;33;31];
x = A^-1*b
cond(A)
norm(trueX-x,inf)/norm(trueX,inf)
norm(trueA-A,inf)/norm(trueA,inf)
+39
View File
@@ -0,0 +1,39 @@
#1.b
nodes = linspace(-2,4,10);
f=@(x)(x+1)./(3*x.^2+2*x+1);
plot(nodes, f(nodes),'o');
hold on
x=linspace(-2,4,500);
plot(x,f(x));
hold on
lnf=lagrange_classic(nodes,f(nodes),x);
plot(x,lnf)
plot(x,abs(f(x)-lnf),'x')
nodes = [81,100,121];
f_nodes = [9,10,11];
sqrt(118);
lnf = lagrange_classic(nodes,f_nodes, [118]);
nodes = [1980,1990,2000,2010,2020];
f_nodes = [4451,5287,6090,6970,7821];
lnf = lagrange_barycentric(nodes, f_nodes, [2005,2015])
abs([6474,7405] - lnf)
+32
View File
@@ -0,0 +1,32 @@
#truex = linspace(0,1,20);
#x0=[0,1/3,1/2,1]
#f=@(x)cos(pi*x);
#plot(x0, f(x0),'o');
#hold on
#plot(truex, f(truex),'blue');
#lnf = newton(x0,f(x0),truex)
#plot(truex, lnf, 'r');
#lnf = newton(x0,f(x0),1/5)
#truex = linspace(1001,1009,9);
#x0=[1000,1010,1020,1030,1040,1050];
#f0=[3.000000,3.0043214,3.0086002,3.0128372,3.0170333,3.0211893];
#lnf = newton(x0,f0,truex)
x0 = linspace(-4,4,9);
f=@(x)power(2,x);
lnf = aitken(x0,f(x0),1/2)
+63
View File
@@ -0,0 +1,63 @@
nodes = linspace(-2,4,7);
f=@(x)(x+1)./(3*x.^2+2*x+1);
#plot(nodes, f(nodes),'o');
#hold on
x=linspace(-2,4,500);
#plot(x,f(x));
hold on
lnf=lagrange_classic(nodes,f(nodes),x);
#plot(x,lnf)
boor = spline(nodes,f(nodes),x);
#plot(x,boor)
df = @(x)(-(3*x.^2+6*x+1)./(3*x.^2+2*x+1).^2);
herm = hermite(nodes,f(nodes), df(nodes),x)
#plot(x,herm)
f=@(x)(x.*sin(pi.*x));
nodes = [-1,-1/2,0,1/2,1,3/2];
x=linspace(-1,3/2,500);
boor = spline(nodes,f(nodes),x);
boor_comp = spline(nodes,[pi,f(nodes),-1],x)
piece = pchip(nodes,f(nodes),x)
#plot(nodes, f(nodes),'o');
#hold on
#plot(x,boor);
#plot(x, boor_comp);
#plot(x, piece);
nodes = [0.5,1.5,2,3,3.5,4.5,5,6,7,8];
f_nodes = [5,5.8,5.8,6.8,6.9,7.6,7.8,8.2,9.2,9.9];
x=linspace(0.5,8,500);
p = polyfit(nodes,f_nodes,1);
plot(nodes, f_nodes,'o');
hold on
plot(x, polyval(p,x))
norm(p)
polyval(p,4)
@@ -0,0 +1,10 @@
function I = adquad(f, a, b, err, m)
I1 = trapz_int(f, a, b, m);
I2 = trapz_int(f, a, b, 2*m);
if abs(I1 - I2) < err % success
I = I2;
return
else % recursive subdivision
I = adquad(f, a, (a+b)/2, err, m) + adquad(f, (a+b)/2, b, err, m);
end
end;
@@ -0,0 +1,12 @@
function Lnf = aitken(x0,f0,x)
p = zeros(length(x0));
p(:,1) = f0'
n = length(x0)
for i=2:n
for j=2:i
p(i,j) = (1/(x0(i)-x0(i-j+1)))*det([x-x0(i-j+1), p(i-1,j-1);x-x0(i),p(i,j-1)]);
endfor
endfor
Lnf=p(n,n);
end;
@@ -0,0 +1,11 @@
function t = backdiff(f)
n = length(f);
t = zeros(n);
t(:,1)= f'
for j = 2:n
t(j:n,j) = diff(t(j-1:n,j-1));
endfor
end;
@@ -0,0 +1,10 @@
function x = backsub(U, b)
n = length(b);
x = zeros(size(b));
for k = n:-1:1
x(k) = (b(k) - (U(k,k+1:n) * x(k+1:n)))/U(k,k);
endfor
end;
@@ -0,0 +1,11 @@
function t = divdiff(x, f)
n = length(x);
t=zeros(n);
t(:,1)= f'
for j = 2:n
t(1:n-j+1,j) = (t(2:n-j+2,j-1) - t(1:n-j+1,j-1))./((x(j:n)-x(1:n-j+1))');
endfor
end
@@ -0,0 +1,13 @@
function [z, t] = divdiff2(x,f,df)
z = repelem(x,2);
lz = length(z);
f2 = repelem(f,2);
t=zeros(lz);
t(:,1)= f2';
t(1:2:lz-1,2) = df';
t(2:2:lz-2,2) = (diff(f)./diff(x))';
for j = 3:lz
t(1:lz-j+1,j) = (t(2:lz-j+2,j-1) - t(1:lz-j+1,j-1))./((z(j:lz)-z(1:lz-j+1))');
endfor
end
@@ -0,0 +1,11 @@
function t = forwarddiff(f)
n = length(f);
t = zeros(n);
t(:,1)= f'
for j = 2:n
t(1:n-j+1,j) = diff(t(1:n-j+2,j-1));
endfor
end;
@@ -0,0 +1,10 @@
function x = forwsub(U, b)
n = length(b);
x = zeros(size(b));
for k = 1:n
x(k) = (b(k) - (U(k,1:k-1) * x(1:k-1)))/U(k,k);
endfor
end;
+18
View File
@@ -0,0 +1,18 @@
function x = gauss(A, b)
[r,n] = size(A);
x = zeros(size(b));
A = [A,b]
for i = 1:n
[v, p] = max(abs(A(i:n, i)));
p = p + i - 1;
A([i p],:) = A([p i],:);
for j = i+1:n
coeff = A(j, i)/ A(i,i);
A(j, i:n+1) = A(j, i:n+1) - coeff * A(i,i:n+1)
endfor
endfor
x = backsub(A(:,1:n),A(:,n+1));
end;
@@ -0,0 +1,20 @@
function [x, nit] = gausssidel(A, b, x0, err, maxnit)
n = length(b);
D = diag(diag(A));
L = tril(A, -1);
U = A - D - L;
M = D + L;
N = -U;
T = M^(-1)*N;
c = M^(-1)*b;
x = x0;
for k = 1:maxnit
old_x = x;
x = T*x+c;
if norm(x-old_x,inf) <= ((1-norm(T,inf))/norm(T,inf))*err
nit = k;
return;
endif
endfor
error('Too dificult');
end;
@@ -0,0 +1,13 @@
function Hnf = hermite(x0,f0,df0,x)
[z,dz] = divdiff2(x0,f0,df0);
x0=z;
ds = dz(1,:);
n = length(ds);
m = length(x);
Hnf = zeros(size(x));
for i=1:m
for j=1:n
Hnf(i) = Hnf(i) + ds(j) * (prod( x(i) - x0([1:j-1])));
endfor
endfor
end;
@@ -0,0 +1,4 @@
function R = homer_int(f, a, b, n)
R = ((b-a)/(2*3*n)) * (f(a)+f(b)+4*sum(f(a+([1:2:2*n-1])*((b-a)/(2*n))))+2*sum(f(a+([2:2:2*n-2])*((b-a)/(2*n)))));
end;
@@ -0,0 +1,17 @@
function [x, nit] = jacobi(A, b, x0, err, maxnit)
n = length(b);
M = diag(diag(A));
N = M - A;
T = M^(-1)*N;
c = M^(-1)*b;
x = x0;
for k = 1:maxnit
old_x = x;
x = T*x+c;
if norm(x-old_x,inf) <= ((1-norm(T,inf))/norm(T,inf))*err
nit = k;
return;
endif
endfor
error('Too dificult');
end;
@@ -0,0 +1,14 @@
function Lnf = lagrange_barycentric(nodes,f_nodes,x)
n = length(nodes);
m = length(x);
Lnf = zeros(size(x));
l = zeros(size(nodes));
for i = 1:m
ux = prod( x(i) - nodes([1:n]));
for j = 1:n
wi(j) = 1/prod( nodes(j) - nodes([1:j-1,j+1:n]));
endfor
Lnf(i) = ux* ((wi./(x(i) - nodes([1:n])))*f_nodes');
endfor
end;
@@ -0,0 +1,13 @@
function Lnf = lagrange_classic(nodes,f_nodes,x)
n = length(nodes);
m = length(x);
Lnf = zeros(size(x));
l = zeros(size(nodes));
for i = 1:m
for j = 1:n
l(j) =(prod( x(i) - nodes([1:j-1,j+1:n])))/(prod( nodes(j) - nodes([1:j-1,j+1:n])));
endfor
Lnf(i) = l*f_nodes';
endfor
end;
@@ -0,0 +1,13 @@
function Lnf = newton(x0,f0,x)
d = divdiff(x0,f0);
ds = d(1,:);
n = length(ds);
m = length(x);
Lnf = zeros(size(x));
for i=1:m
for j=1:n
Lnf(i) = Lnf(i) + ds(j) * (prod( x(i) - x0([1:j-1])));
endfor
endfor
end;
@@ -0,0 +1,4 @@
function R = rectangle_int(f, a, b, n)
R = ((b-a)/n) * sum(f(a+([0:n-1]+1/2)*((b-a)/n)))
end;
@@ -0,0 +1,4 @@
function R = trapz_int(f, a, b, n)
R = ((b-a)/(2*n)) * (f(a)+f(b)+2*sum(f(a+([1:n-1])*((b-a)/n))));
end;