MUST BE IN MATLAB. please comment on the code incase I need to correct something
ID: 3803821 • Letter: M
Question
MUST BE IN MATLAB. please comment on the code incase I need to correct something later need the answer this are the only one I have left couldn't find a decently commented code or in some cases even the code please and thank you-------------------------------------------------------------
2.Need an interactive Bairstow funcion that asks user for the function and for the other variables even iterations and displays it in the command window as seen in the top image
------------------------------------------------------------- 3. Need an interactive Muller funcion that ask the user for the function and the other variable including iterations and displays it in the command window as show in the upper image
Define a function for secant method functio my secant secant f inline input ('Insert function 's')) input Enter lower bound Xl x1 input Enter pper bound xu tol input ('Al able tolerance es
Explanation / Answer
MATLAB Code:
function y=mysecant()
%% take Input From User
f=inline(input('Insert Function: ','s'));
x0=input('Enter lower bound xl = ');
x1=input('Enter Upper bound xu = ');
tol=input('Allowable tolerance es = ');
%% Assign values
a=x0;
b=x1;
flag =1;
%% Initial Guess
c = (a*f(b) - b*f(a))/(f(b) - f(a));
%% Only for Display Purpose
disp(' Step X f');
disp([flag c f(c)]);
%% Iterate Until error is acceptable
while abs(f(c)) > tol
a = b;
b = c;
c = (a*f(b) - b*f(a))/(f(b) - f(a));
disp([flag c f(c)]);
flag = flag + 1;
%% For maximum iteration allowed
if(flag == 100)
break;
end
end
display(['Root is x = ' num2str(c)]);
y = c;
end
OUTPUT:
>> mysecant
Insert Function: x^3-3*x+1
Enter lower bound xl = 0
Enter Upper bound xu = 1
Allowable tolerance es = 10^-15
Step X f
1.0000 0.5000 -0.3750
1.0000 0.2000 0.4080
2.0000 0.3563 -0.0237
3.0000 0.3477 -0.0011
4.0000 0.3473 0.0000
5.0000 0.3473 -0.0000
6.0000 0.3473 -0.0000
Root is x = 0.3473
ans =
0.3473
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.