Hi, hope you all are well. I am here due to a problem with the function called 'fzero'.
On one hand, I have this file: f.m
function y=f(x)
y= besselj(1,x);
And on the other hand: exercise.m
D=fzero(@f,[0 10]);
tam=length(D);
for i=1:tam
fprintf(' Possible MAX or MIN: f(%f) \n',D(i))
end
When I run exercise.m, I get this output:
Possible MAX or MIN: f(0.000000)
I know where this values should be located...
What am I doing wrong? Thanks in advance. PD: I have to use fzero and besselj(1,x) at [0:10]

 採用された回答

Matt J
Matt J 2015 年 6 月 30 日
編集済み: Matt J 2015 年 6 月 30 日

0 投票

Looks like the code succeded to me. Your function does have a root at x=0.
No idea why you display a message involving MAX and MIN unless you were really trying to minimize/maximize the function. But as its name implies, fzero finds zeros, not max's and min's. You might have been looking to use fminsearch()

1 件のコメント

JM
JM 2015 年 6 月 30 日
Thanks for your answer. Due to that I realized I should have used f'(x) instead of f(x). Here it is the fixed code:
x=0:10;
xx=0:0.01:10;
y= @(x) besselj(1,x); % F(X)
f= @(x) (besselj(0, x) - besselj(1, x)/x); % F'(X)
for i=2:10
D=fzero(f,x(i));
fprintf(' Possible MAX or MIN: f(%f) \n',D)
end
It gives you all the possible max's and min's (absolutes and relatives) of F(X) evaluated at [0:10] but at 0 and 10. Thanks again. Regards

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

JM
2015 年 6 月 30 日

コメント済み:

JM
2015 年 6 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by