Hello guys i need help

1 回表示 (過去 30 日間)
Ted Erdenekhuyag
Ted Erdenekhuyag 2020 年 6 月 5 日
回答済み: Steven Lord 2020 年 6 月 5 日
Attempt to find zero-crossing points of the following equation f(x)=x^3-2x^2 sinx+5xcosx+1/x within [0.5, 4] (Using fzero function).
function y=f(x)
y=x.^3-2*x.^2*sin(x)+5*x*cos(x)+1/x;
fun = @f; % function
x0 = [0.5 4]; % initial interval
x = fzero(fun,x0)
not working pls help me

回答 (1 件)

Steven Lord
Steven Lord 2020 年 6 月 5 日
Don't try to call fzero with a function handle to the function containing the fzero call as the input. If you do, the function will call fzero which will call the function which will call fzero which will call the function which will call fzero ... until eventually you reach the recursion limit and MATLAB throws an error or you exhaust the stack space available to MATLAB and MATLAB and/or your computer crashes.
Call fzero from a different function than the one you're trying to solve. I'm going to return the function handle from the "different function" so you can check the answer. I also had to change the interval, since your function was positive for both x = 0.5 and x = 4 which caused fzero to throw an error. Plotting your function shows a zero between x = 1 and x = 2 (and the sign of the function value is different for those two points.)
function [x, fun] = example542639
fun = @f; % function
x0 = [1 2]; % initial interval
x = fzero(fun,x0);
function y=f(x)
y=x.^3-2*x.^2*sin(x)+5*x*cos(x)+1/x;
How to use this?
[x, fun] = example543639; % call the function
result = fun(x) % Check that x is a zero of fun, result should be small in magnitude

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by