Matlab新手二分查找故障排除

发布于 2024-11-28 09:07:37 字数 1026 浏览 0 评论 0原文

总的来说,我是 Matlab/编程的新手。我希望编写一个程序/脚本,使用递归二分搜索来近似 $2x - 3sin(x)+5=0$ 的根,这样一旦截断误差肯定是 $<<,迭代就会终止。 0.5 \times 10 ^{-5}$ 并打印出迭代次数以及根的估计。

这是我的尝试,似乎破坏了我的计算机......

%Approximating the root of f(x) = 2*x  - 3*sin(x) + 5 by binary search

%Define variables

low = input('Enter lower bound of range: ');

high = input('Enter upper bound of range: ');

mid = (low + high)/2;


%Define f_low & f_high

f_low = 2*low  - 3*sin(low) + 5;

f_high = 2*high  - 3*sin(high) + 5;

f_mid = 2*mid  - 3*sin(mid) + 5;


%Check that the entered range contains the key

while (f_low * f_high) > 0 || low > high

     disp('Invalid range')

     low = input('Enter lower bound of range: ');

     high = input('Enter upper bound of range: ');

end



%The new range

while abs(f_mid) > 0.5*10^(-5)



    if f_mid < 0

     low = mid;



    elseif f_mid > 0

      high = mid;

    end   



end



fprintf('mid = %.4f \n', mid)

我什至没有添加迭代次数计数位(我不太确定该怎么做),并且我已经被卡住了。

感谢您的任何帮助。

I am a newbie to Matlab/programming in general. I wish to write a program/script that uses recursive binary search to approximate the root of $2x - 3sin(x)+5=0$, such that the iteration terminates once the truncation error is definitely $< 0.5 \times 10 ^{-5}$ and print out the number of iterations as well as the estimate of the root.

Here is my attempt that seems to have broken my computer...

%Approximating the root of f(x) = 2*x  - 3*sin(x) + 5 by binary search

%Define variables

low = input('Enter lower bound of range: ');

high = input('Enter upper bound of range: ');

mid = (low + high)/2;


%Define f_low & f_high

f_low = 2*low  - 3*sin(low) + 5;

f_high = 2*high  - 3*sin(high) + 5;

f_mid = 2*mid  - 3*sin(mid) + 5;


%Check that the entered range contains the key

while (f_low * f_high) > 0 || low > high

     disp('Invalid range')

     low = input('Enter lower bound of range: ');

     high = input('Enter upper bound of range: ');

end



%The new range

while abs(f_mid) > 0.5*10^(-5)



    if f_mid < 0

     low = mid;



    elseif f_mid > 0

      high = mid;

    end   



end



fprintf('mid = %.4f \n', mid)

I haven't even added in the number-of-iterations counting bit (which I am not quite sure how to do) and already I am stuck.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2024-12-05 09:07:37

一旦设置high=mid或low=mid,mid和f_mid是否会重新计算?如果 f_low>0 且 f_high<0,看起来你会失败。这是一个有效的条件,但在这种情况下您选择了错误的重置条件。此外,您的终止检查是针对函数值,而不是低值和高值之间的差异。这可能是您想要的,或者您可能想检查这两种方式。对于非常平坦的函数,您可能无法获得那么小的函数值。

Once you set high=mid or low=mid, is mid and f_mid recalculated? It looks like you will fail if f_low>0 and f_high<0. This is a valid condition, but you are choosing the wrong one to reset in this case. Also, your termination check is on the function value, not the difference between low and high. This may be what you want, or maybe you want to check both ways. For very flat functions you may not be able to get the function value that small.

污味仙女 2024-12-05 09:07:37

您不需要 f_mid,这实际上会误导您。您只需要计算每一步的值,然后看看该往哪个方向走。

另外,您只是更改低值和高值,但不会再次评估 f_lowf_high。 Matlab不是代数系统(有符号计算的模块,但那是另一回事),所以你没有定义f_lowf_high随着low的变化而变化和高:你必须在最后的循环中重新评估它们。

You don't need f_mid, and is in fact misleading you. You just need to calculate the value at each step, and see which direction to go.

Plus, you are just changing low and high, but you do not evaluate again f_low or f_high. Matlab is not an algebra system (there are modules for symbolic computation, but that's a different story), so you did not define f_low and f_high to change with the change of low and high: you have to reevaluate them in your final loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文