Reputation: 1
I am trying to solve the following question:
Use the bisection method to find solutions accurate to within
10^−4
on the interval[−5, 5]
of the following functions:f(x)= x^5-10x^3-4
This is my code:
function sol=bisect(fn,a,b,tol)
%Bisection method for the Nonlinear Function
fa=feval(fn,a);fb=feval(fn,b);
if fa*fb>0;fprintf('Endpoints have same sign')
return
end
k=0;
while abs (b - a)>tol
c =(a+b)/2;
fc=feval(fn,c);
if fa*fc < 0; b=c; else a = c;
k=k+1;
end
end
sol=(a+b)/2;
When I run the program, I do:
a= -5
b=5
fn = x^5-10x^3-4
But the last line returns an error:
undefined function or variable x
Upvotes: 0
Views: 838
Reputation: 35525
to define an equation that can be evaluated by feval
, you need to define as a function.
Try defining fn
as fn=@(x)(x^5-10x^3-4)
.
This way you can use feval(fn,3)
.
Upvotes: 1