calling function return only one iteration answer
1 view (last 30 days)
Show older comments
Asaduzzaman Sarker
on 18 Feb 2019
Commented: Asaduzzaman Sarker
on 18 Feb 2019
I have this sacent method function. it works fine while testing it.
function [root, ea, iter] = secant(f, xi, xs, es, maxit)
iter=0;
while(true)
root = xi - (f(xi) * (xi-xs))/(f(xi)-f(xs));
xs = xi;
xi=root;
iter=iter+1;
if root~=0
ea= abs((xi-xs)/xi)*100;
end
if iter >=maxit || ea<=es
break;
end
end
end
But when i call it like following it only returns 1st iteration values.
[root, ea, iter]= secant(@(x)x^2-9,2,5,0,.01,5)
Output:
root = 2.7143
ea = 26.316
iter = 1
But original output:
root = 3.0000
ea = 0.0010005
iter = 5
What am i doing wrong?
0 Comments
Accepted Answer
Walter Roberson
on 18 Feb 2019
You are passing in 6 inputs to a function that expects 5. Current versions of MATLAB (as far back as I can recall) would refuse to run the function.
I suspect that instead of 0,.01 you intended 0.01
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!