Create a function that will use the Secant method to try to find a root. Because
ID: 3555933 • Letter: C
Question
Create a function that will use the Secant method to try to find a root. Because this method may diverge, you should limit the number of time the loop repeats.
Explanation / Answer
% Find the roots of using Secant Method % func --> function is taken by user % like sin(x) or x^2 + x - 1 or any other but use the same format % i.e. use paranthesis as used above with 'x' for sin, cos,... etc % and spacing between '+' or '-' operation % a --> Xn-1 % b --> Xn % c --> Xn+1 % maxerr --> Maximum Error in Root syms x; flag = 1; func = input('Enter Function in terms of x: '); a = input('Ener Lower Limit: '); b = input('Ener Upper Limit: '); maxerr = input('Enter Maximum Error: '); f = inline(func); c = (a*f(b) - b*f(a))/(f(b) - f(a)); disp(' Xn-1 f(Xn-1) Xn f(Xn) Xn+1 f(Xn+1)'); disp([a f(a) b f(b) c f(c)]); while abs(f(c)) > maxerr a = b; b = c; c = (a*f(b) - b*f(a))/(f(b) - f(a)); disp([a f(a) b f(b) c f(c)]); flag = flag + 1; if(flag == 100) break; end end display(['Root is x = ' num2str(c)]);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.