Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Sturm's method
- clc; clear; close all;
- f = @(x) x.^3 - 5*x + 4;
- df = @(x) 3*x.^2 - 5;
- x = -4:0.01:4;
- F = [1, 0, -5, 4];
- DF = [3, 0, -5];
- L = length(F);
- figure(1)
- plot(x,f(x)); hold on; grid on;
- plot(roots(F),f(roots(F)),'ro'); hold off;
- a=0;
- b=4;
- c_a = 0; %counter_a
- c_b = 0; %counter b
- ax_1 = f(a);
- bx_1 = f(b);
- ax_2 = df(a);
- bx_2 = df(b);
- if ax_1*ax_2 < 0
- c_a = c_a + 1;
- end
- if bx_1*bx_2 < 0
- c_b = c_b + 1;
- end
- ax_1 = ax_2;
- bx_1 = bx_2;
- for n=1:(L-2)
- [q,r] = deconv(F, DF)
- L_x = length(F)-length(DF);
- r2 = -1.*r(end-L_x:end);
- ax_2 = polyval(r2, a);
- bx_2 = polyval(r2, b);
- if ax_1*ax_2 < 0
- c_a = c_a + 1;
- end
- if bx_1*bx_2 < 0
- c_b = c_b + 1;
- end
- ax_1 = ax_2;
- bx_1 = bx_2;
- F = DF;
- DF = r2;
- end
- How_many_roots = c_a - c_b
- ````````````````````````````````
- %secant method 1
- clc; clear; close all;
- dx = 0.01;
- x = -10:dx:10;
- f = @(x) x.^2-4;
- y=f(x);
- k = 100; %number of repetitions
- q = 5; %starting position
- a = 1; %frst limit
- b = 5; %second limit
- for n=1:k
- if f(q) == 0
- display("yep, that's the one")
- break
- elseif (f(a)*f(q))<0
- b = q;
- q = a - ((f(a)*(b-a))/(f(b)-f(a)));
- else
- a = q;
- q = b - ((f(b)*(b-a))/(f(b)-f(a)));
- end
- if abs(0-f(q))<0.000000001
- q
- break
- end
- end
- ``````````````````````````````````````````
- %secant method 2
- clc; clear; close all;
- dx = 0.01;
- x = -10:dx:10;
- f = @(x) x.^2-4;
- k = 100; %number of repetitions
- a = 1; %frst limit
- b = 5; %second limit
- gate = 0;
- %coefficients=polyfit([0,1],[0,2],1)
- %roots(coefficients)
- for n=1:k
- cf = polyfit([a,b], [f(a),f(b)], 1);
- x0 = roots(cf);
- if gate == 0
- cf2 = polyfit([x0, b], [f(x0),f(b)], 1);
- a = roots(cf2);
- gate = 1;
- fin = a;
- else
- cf2 = polyfit([a, x0], [f(a),f(x0)], 1);
- b = roots(cf2);
- gate = 0;
- fin = b;
- end
- if abs(0 - f(fin))<0.0000000001
- fin
- break
- end
- end
- `````````````````````````````````````````````````````````````
- %secant method 3
- clc; clear; close all;
- dx = 0.01;
- x = -100000:dx:100000;
- f = @(x) x.^3-11*x.^2+14*x+80;
- k = 1; %number of repetitions
- a = -6; %frst limit
- b = 10; %second limit
- c = (a+b)/2
- for n=1:k
- f_ex = (f(c)+(sign(f(b))*sqrt((f(c)^2)-(f(a)*f(b)))))/f(b)
- xa = f(a)
- xb = f(b)*(f_ex^2)
- xc = f(c)*f_ex
- x1 = xc + (xc - xa)*(f(xc)*sign(f(xa)-f(xb)))/nthroot((f(xc)^2)-(f(xa)*f(xb)),2)
- b = x1;
- c = (a+b)/2;
- if abs(0-f(b))<0.0000001
- b
- break
- end
- b
- end
- ``````````````````````````````````````````
- %Newton method
- clc; clear; close all;
- dx = 0.01;
- x = -10:dx:10;
- x2 = 1:dx:10;
- f = @(x) x.^2 - 4;
- df1 = @(x) 2*x;
- df2 = @(x) 2+0*x;
- k = 50; %number of repetitions
- a = 1; %frst limit
- b = 10; %second limit
- for n=1:k
- if f(a)*df2(a) > 0
- x1 = a - (f(a)/df1(a));
- a = x1;
- else
- x1 = b - (f(b)/df1(b));
- b = x1;
- end
- if abs(0-f(x1))<0.000000000001
- x1;
- break
- end
- end
- figure(1)
- plot(x2,f(x2)); hold on; grid on;
- plot(x1,f(x1),'ro'); hold off;
- ````````````````````````````````
- %consecutively improved approximation
- clc; clear; close all;
- %f = exp(x) - (1/x)
- f2 = @(x) x - (1/4)*(exp(x)-(1/x))
- k = 1/4
- x=3/4;
- A=zeros(1,20);
- n=length(A);
- for k=1:n
- A(k) = f2(x);
- x=f2(x)
- end;
- figure(1);
- plot(A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement