Calling function inside function input
Show older comments
I've been trying to run a program that finds the root of a function following the bisection method, given an interval (a,b), tolerance level and maximum iterations for the method. I want to be able to run custom functions, but I cannot seem to find a way to figure it out. I don't know if I'm even chasing the right answer.
I have previously stated my custom function as:
function [y]=fun(x)
y=x-sin(x)-1
end
And then ran my main program like this, using the variables:
z=linspace(0,5,1000);funcion=fun(z);a=1;b=3;tol=0.0001;maxiter=5;
function [x,it]=bisecc(funcion,a,b,tol,maxiter)
for it=1:maxiter
c=(a+b)/2;
if sign(funcion(a))~=sign(funcion(c))
b=c;
else
a=c;
end
if abs(funcion(c))<tol
break
end
end
plot(linspace(a,b,length(funcion)),funcion), grid on
x=c;
[x_]=fzero(funcion,(a+b)/2);
fprintf('x(fzero)=%f',x_)
However, foolish of me, funcion(*whatever*) just indexes from the matrix that I created, and I spent an embarrasing amount of time until I noticed.
What can I do? Thanks in advance.
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!