二分法matlab编程代码及注释(MATLAB二分法求方程的近似解)

matlab二分法求解实例关注:95答案:2mip版解决时间2021-01-2801:44提问者人潮拥挤你不在2021-01-2718:57求函数f=x^3+2*x^2+x-5(-2,2)为区间起点和终点,10^-4为精度>>a=-2;b=2;x=a:b;f=@(x)x^3+2*x^2+x-5;c=(a+b)./2;whileabs(b-a)>1e-6i…

大家好,又见面了,我是你们的朋友全栈君。

matlab二分法求解实例

关注:95  答案:2  mip版

解决时间 2021-01-28 01:44

e6cb1a03ad541b3098697807b7bf1798.png

提问者人潮拥挤你不在

2021-01-27 18:57

求函数 f=x^3+2*x^2+x-5 (-2,2)为区间起点和终点,10^-4为精度

>> a=-2;

b=2;

x=a:b;

f = @(x)x^3+2*x^2+x-5;

c=(a+b)./2;

while abs(b-a)>1e-6

if f(c)*f(b)<0

a=c;

else

b=c;

end

c=(a+b)./2;

x=c;

end

fprintf(‘\n x = %.5f, f(x) = %.5f \n’, x, f(x));

如果区间有两个,分别有一个零点,即x1,x2;需要怎么改动输出两个零点值啊

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家青春敷年華

2021-01-27 19:25

那调用两次不就ok了。区间你是用a、b定义的嘛。

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼雨落轻尘

2021-01-27 19:40

>> f=inline(‘x^2-x-2’);

>> [c,err,yc]=bisect(f,0,3,0.01)

c =

2.0010

err =

0.0059

yc =

0.0029

———–

%使用二分法 求解上面超越方程

%下面是二分法的函数文件,你直接设置输入参数就可以了

function [c,err,yc]=bisect(f,a,b,delta)

%input – f is the function

% – a and b are the left and right endpoints

% – delta is the tolerance

%output – c is the zero

% – yc= f(c)

% – err is the error estimate for c

%if f is defined as an m-file function use the @ notation

% call [c,err,yc]=bisect(@f,a,b,delta).

%if f is defined as an anonymous function use the

% call [c,err,yc]=bisect(f,a,b,delta).

% numerical methods: matlab programs

% (c) 2004 by john h. mathews and kurtis d. fink

% complementary software to accompany the textbook:

% numerical methods: using matlab, fourth edition

% isbn: 0-13-065248-2

% prentice-hall pub. inc.

% one lake street

% upper saddle river, nj 07458

ya=f(a);

yb=f(b);

if ya*yb > 0,return,end

max1=1+round((log(b-a)-log(delta))/log(2));

for k=1:max1

c=(a+b)/2;

yc=f(c);

if yc==0

a=c;

b=c;

elseif yb*yc>0

b=c;

yb=yc;

else

a=c;

ya=yc;

end

if b-a < delta, break,end

end

c=(a+b)/2;

err=abs(b-a);

yc=f(c);

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128113.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号