使用二分法在Matlab中找到解决方案? [英] Use the bisection method to find solutions in Matlab?

查看:427
本文介绍了使用二分法在Matlab中找到解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决以下问题:

I am trying to solve the following question:

使用二等分方法在以下函数的间隔[−5, 5]内找到精确到10^−4内的解决方案:

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

这是我的代码:

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;

运行程序时,我会做:

a= -5
b=5
fn =  x^5-10x^3-4

但是最后一行返回错误:

But the last line returns an error:

未定义的函数或变量x

undefined function or variable x

推荐答案

要定义可由feval求值的方程,您需要将其定义为一个函数.

to define an equation that can be evaluated by feval, you need to define as a function.

尝试将fn定义为fn=@(x)(x^5-10x^3-4). 这样您就可以使用feval(fn,3).

Try defining fn as fn=@(x)(x^5-10x^3-4). This way you can use feval(fn,3).

这篇关于使用二分法在Matlab中找到解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆