Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ ] = newton(func, a, b, delta, maxd )
- %UNTITLED Summary of this function goes here
- % Detailed explanation goes here
- funcd1 = diff(func,1);
- funcd2 = diff(func,2);
- eps = 10^-delta;
- set = 0;
- x0 = 0;
- if a > b
- [a,b] = deal(b,a);
- end
- if subs(func,a)*subs(funcd2,a) > 0
- x0 = a;
- set = 1;
- else
- if subs(func,b)*subs(funcd2,b) > 0
- x0 = b;
- set = 1;
- end
- end
- if set == 0
- for i=a:eps:b
- if subs(func,i)*subs(funcd2,b) > 0
- x0 = i;
- set = 1;
- end
- end
- end
- if set == 0
- sprintf('Nie mozna znalezc punktu spelniajacego warunek f(x)*f"(x)>0!');
- return
- else
- sprintf('Znaleziono x0=%f',x0)
- end
- xk = x0;
- xk1 = x0 - subs(func,x0)/subs(funcd1,x0);
- d = 0;
- while abs(subs(func,xk1)) > eps && d < maxd
- d = d+1;
- xk = xk1;
- xk1 = xk - subs(func,xk)/subs(funcd1,xk);
- if abs(xk1-xk) <= eps || abs(subs(func,xk1)) <= eps
- break
- end
- end
- if d == maxd
- sprintf('Nie znaleziono miejsca zerowego w zadanym przedziale po %d przejsciach',maxd)
- else
- sprintf('x0 = %f',xk);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement