Advertisement
Guest User

ferrari/derivative_aprox/derivative/cardano

a guest
Jan 21st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. %ferrari
  2.  
  3. clc; clear; close all;
  4.  
  5. f = [1, 2, 3, 4]; %we supose that "a" is 1
  6. f2 = [1, 1, 2, 3, 4]; %f1 but with "a"
  7.  
  8. a3=f(1); a2=f(2); a1=f(3); a0=f(4);
  9.  
  10. B(1) = 1; %we have to add this to be able to use cardano function
  11. B(2) = -a2/2;
  12. B(3) = (a3*a1 - 4*a0)/4;
  13. B(4) = (4*a2*a0 - (a3^2)*a0 -a1^2)/8;
  14. z = cardano(B)
  15. k=z(3);
  16. p = sqrt(2*k +(a3^2)/4 - a2);
  17. q = (k*a3 - a1)/(2*p);
  18. D1 = (a3/2 + p)^2 - 4*(k + q); %here we have some simple delta. surprising isnt it?
  19. D2 = (a3/2 - p)^2 - 4*(k - q);
  20. x1= ( -(a3/2 + p) - D1^(1/2) )/2;
  21. x2 = ( -(a3/2 + p) + D1^(1/2) )/2;
  22. x3 = ( -(a3/2 - p) - D2^(1/2) )/2;
  23. x4 = ( -(a3/2 - p) + D2^(1/2) )/2;
  24. X = [x1; x2; x3; x4;]
  25.  
  26. R = roots(f2)
  27. figure(1);
  28. plot(R,'o'); hold on; grid on;
  29. plot(X,'*r'); hold off;
  30.  
  31. ``````````````````````````````````````````````````````````````
  32. %derivative approximation
  33.  
  34. clc; clear; close all;
  35.  
  36. dx =0.01;
  37. x = -2:dx:2;
  38.  
  39. f = @(x) x.^2-3;
  40. fx = [1,0,-3];
  41.  
  42. n = length(fx)-1; %polynomial order
  43. df3 =(n:-1:1).*fx(1:end-1)
  44.  
  45. V = polyval(df3,x);
  46.  
  47. y=f(x);
  48. df1 = (y(2:end)-y(1:end-1))/dx;
  49. df2 = (df1(1:end-1)+df1(2:end))/2;
  50.  
  51. figure(1)
  52. plot(x(1:end-2),df1(1:end-1)); hold on; grid on;
  53. plot(x(1:end-2),df2,'b-');
  54. plot(x(1:end-2),V(1:end-2),'o'); %diferences are here because of the shift
  55.  
  56.  
  57.  
  58.  
  59. ````````````````````````````````````````````````````````````````````````````
  60. %derivative
  61.  
  62. clc; clear; close all;
  63. %f = x.^2+2*x+3
  64. A = [1, 2, 3];
  65. A = A(:).';
  66. n = length(A)-1; %polynomial order
  67. b = (n:-1:1).*A(1:end-1)
  68.  
  69.  
  70.  
  71.  
  72. ````````````````````````````````````````````````````````````````````````````
  73. %cardanos method
  74.  
  75. clc; clear; close all;
  76.  
  77.  
  78. %f= @(x) 1*x.^3+ 2*x.^2 + 3*x + 4;
  79.  
  80. f = [1, 2, 3, 4];
  81.  
  82. a3=f(1); a2=f(2); a1=f(3); a0=f(4);
  83. p = (3*a1-((a2)^2))/9;
  84. q = ((a2^3)/27) - ((a1*a2)/6) + (a0/2);
  85. D = q^2 + p^3
  86.  
  87. if D>= 0
  88. u = nthroot((-q + sqrt(D)),3);
  89. v = nthroot((-q - sqrt(D)),3);
  90. Y(1)= u+v;
  91. Y(2) = -0.5*(u+v) + ((j*sqrt(3))/2)*(u-v);
  92. Y(3) = -0.5*(u+v) - ((j*sqrt(3))/2)*(u-v);
  93. else
  94. fi = acos(-q/sqrt(-p^3));
  95. Y(1) = 2* sqrt(-p) * cos ((fi)/3);
  96. Y(2) = 2* sqrt(-p) * cos ((fi + 2*pi)/3);
  97. Y(3) = 2* sqrt(-p) * cos ((fi + 4*pi)/3);
  98. end
  99.  
  100. X(1) = Y(1) - a2/3;
  101. X(2) = Y(2) - a2/3;
  102. X(3) = Y(3) - a2/3;
  103.  
  104. R = roots(f)
  105.  
  106. figure(1);
  107. plot(R,'o'); hold on; grid on;
  108. plot(X,'*r'); hold off;
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. ```````````````````````````````````
  122. function X = cardano(f)
  123.  
  124. a3=f(1); a2=f(2); a1=f(3); a0=f(4);
  125. p = (3*a1-((a2)^2))/9;
  126. q = ((a2^3)/27) - ((a1*a2)/6) + (a0/2);
  127. D = q^2 + p^3;
  128.  
  129. if D>= 0
  130. u = nthroot((-q + sqrt(D)),3);
  131. v = nthroot((-q - sqrt(D)),3);
  132. Y(1)= u+v;
  133. Y(2) = -0.5*(u+v) + ((j*sqrt(3))/2)*(u-v);
  134. Y(3) = -0.5*(u+v) - ((j*sqrt(3))/2)*(u-v);
  135. else
  136. fi = acos(-q/sqrt(-p^3));
  137. Y(1) = 2* sqrt(-p) * cos ((fi)/3);
  138. Y(2) = 2* sqrt(-p) * cos ((fi + 2*pi)/3);
  139. Y(3) = 2* sqrt(-p) * cos ((fi + 4*pi)/3);
  140. end
  141.  
  142. X(1) = Y(1) - a2/3;
  143. X(2) = Y(2) - a2/3;
  144. X(3) = Y(3) - a2/3;
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement