Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %ferrari
- clc; clear; close all;
- f = [1, 2, 3, 4]; %we supose that "a" is 1
- f2 = [1, 1, 2, 3, 4]; %f1 but with "a"
- a3=f(1); a2=f(2); a1=f(3); a0=f(4);
- B(1) = 1; %we have to add this to be able to use cardano function
- B(2) = -a2/2;
- B(3) = (a3*a1 - 4*a0)/4;
- B(4) = (4*a2*a0 - (a3^2)*a0 -a1^2)/8;
- z = cardano(B)
- k=z(3);
- p = sqrt(2*k +(a3^2)/4 - a2);
- q = (k*a3 - a1)/(2*p);
- D1 = (a3/2 + p)^2 - 4*(k + q); %here we have some simple delta. surprising isnt it?
- D2 = (a3/2 - p)^2 - 4*(k - q);
- x1= ( -(a3/2 + p) - D1^(1/2) )/2;
- x2 = ( -(a3/2 + p) + D1^(1/2) )/2;
- x3 = ( -(a3/2 - p) - D2^(1/2) )/2;
- x4 = ( -(a3/2 - p) + D2^(1/2) )/2;
- X = [x1; x2; x3; x4;]
- R = roots(f2)
- figure(1);
- plot(R,'o'); hold on; grid on;
- plot(X,'*r'); hold off;
- ``````````````````````````````````````````````````````````````
- %derivative approximation
- clc; clear; close all;
- dx =0.01;
- x = -2:dx:2;
- f = @(x) x.^2-3;
- fx = [1,0,-3];
- n = length(fx)-1; %polynomial order
- df3 =(n:-1:1).*fx(1:end-1)
- V = polyval(df3,x);
- y=f(x);
- df1 = (y(2:end)-y(1:end-1))/dx;
- df2 = (df1(1:end-1)+df1(2:end))/2;
- figure(1)
- plot(x(1:end-2),df1(1:end-1)); hold on; grid on;
- plot(x(1:end-2),df2,'b-');
- plot(x(1:end-2),V(1:end-2),'o'); %diferences are here because of the shift
- ````````````````````````````````````````````````````````````````````````````
- %derivative
- clc; clear; close all;
- %f = x.^2+2*x+3
- A = [1, 2, 3];
- A = A(:).';
- n = length(A)-1; %polynomial order
- b = (n:-1:1).*A(1:end-1)
- ````````````````````````````````````````````````````````````````````````````
- %cardanos method
- clc; clear; close all;
- %f= @(x) 1*x.^3+ 2*x.^2 + 3*x + 4;
- f = [1, 2, 3, 4];
- a3=f(1); a2=f(2); a1=f(3); a0=f(4);
- p = (3*a1-((a2)^2))/9;
- q = ((a2^3)/27) - ((a1*a2)/6) + (a0/2);
- D = q^2 + p^3
- if D>= 0
- u = nthroot((-q + sqrt(D)),3);
- v = nthroot((-q - sqrt(D)),3);
- Y(1)= u+v;
- Y(2) = -0.5*(u+v) + ((j*sqrt(3))/2)*(u-v);
- Y(3) = -0.5*(u+v) - ((j*sqrt(3))/2)*(u-v);
- else
- fi = acos(-q/sqrt(-p^3));
- Y(1) = 2* sqrt(-p) * cos ((fi)/3);
- Y(2) = 2* sqrt(-p) * cos ((fi + 2*pi)/3);
- Y(3) = 2* sqrt(-p) * cos ((fi + 4*pi)/3);
- end
- X(1) = Y(1) - a2/3;
- X(2) = Y(2) - a2/3;
- X(3) = Y(3) - a2/3;
- R = roots(f)
- figure(1);
- plot(R,'o'); hold on; grid on;
- plot(X,'*r'); hold off;
- ```````````````````````````````````
- function X = cardano(f)
- a3=f(1); a2=f(2); a1=f(3); a0=f(4);
- p = (3*a1-((a2)^2))/9;
- q = ((a2^3)/27) - ((a1*a2)/6) + (a0/2);
- D = q^2 + p^3;
- if D>= 0
- u = nthroot((-q + sqrt(D)),3);
- v = nthroot((-q - sqrt(D)),3);
- Y(1)= u+v;
- Y(2) = -0.5*(u+v) + ((j*sqrt(3))/2)*(u-v);
- Y(3) = -0.5*(u+v) - ((j*sqrt(3))/2)*(u-v);
- else
- fi = acos(-q/sqrt(-p^3));
- Y(1) = 2* sqrt(-p) * cos ((fi)/3);
- Y(2) = 2* sqrt(-p) * cos ((fi + 2*pi)/3);
- Y(3) = 2* sqrt(-p) * cos ((fi + 4*pi)/3);
- end
- X(1) = Y(1) - a2/3;
- X(2) = Y(2) - a2/3;
- X(3) = Y(3) - a2/3;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement