Use the bisection method to find solutions in Matlab? -
i trying solve following question:
use bisection method find solutions accurate within
10^−4on interval[−5, 5]of following functions:f(x)= x^5-10x^3-4
this code:
function sol=bisect(fn,a,b,tol) %bisection method 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 = c; k=k+1; end end sol=(a+b)/2; when run program, do:
a= -5 b=5 fn = x^5-10x^3-4 but last line returns error:
undefined function or variable x
to define equation can evaluated feval, need define function.
try defining fn fn=@(x)(x^5-10x^3-4). way can use feval(fn,3).
Comments
Post a Comment