Hello guys i need help

1 次查看(过去 30 天)
Ted Erdenekhuyag
Ted Erdenekhuyag 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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by