plot() and fplot() give different results for the same function

32 ビュー (過去 30 日間)
Tamara Macadam
Tamara Macadam 2024 年 2 月 15 日
編集済み: Bruno Luong 2024 年 2 月 15 日
I'm trying to plot the function
between 0 and 100 (not including zero of course), but am running into a problem. When I use
x = linspace(0, 100, 10000);
y = 2.*sqrt(1+x)+((sin(sqrt(x).*pi).*sin(sqrt(1+x).*pi).*(2.*x+1))/(sqrt(x))) - 2.*sqrt(1+x).*cos(sqrt(x).*pi).*cos(sqrt(1+x).*pi);
plot(x,y);
I don't get the result I expected, which is the graph occasionally intersecting y = 0. However, by doing
fplot(@(x) 2.*sqrt(1+x)+((sin(sqrt(x).*pi).*sin(sqrt(1+x).*pi).*(2.*x+1))/(sqrt(x))) - 2.*sqrt(1+x).*cos(sqrt(x).*pi).*cos(sqrt(1+x).*pi), [0 100]);
I do get the result I expected.
I can't just use fplot, as I do need to use the variable y for other things, so I'm not sure what to do or what is even happening here. Any help would be greatly appreciated.

採用された回答

Bruno Luong
Bruno Luong 2024 年 2 月 15 日
編集済み: Bruno Luong 2024 年 2 月 15 日
Your coding is wrong: Replace "/" with "./"
  1 件のコメント
Tamara Macadam
Tamara Macadam 2024 年 2 月 15 日
Oh god I'm silly. Thanks, I should've realised.

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

その他の回答 (1 件)

Torsten
Torsten 2024 年 2 月 15 日
編集済み: Torsten 2024 年 2 月 15 日
x = linspace(0, 100, 10000);
y = 2*sqrt(1+x)+sin(sqrt(pi)*x).*sin(sqrt(1+x)*pi).*(2*x+1)./sqrt(x) - 2*sqrt(1+x).*cos(sqrt(x)*pi).*cos(sqrt(1+x)*pi);
plot(x,y);
hold on
f = @(x)2*sqrt(1+x)+sin(sqrt(pi)*x).*sin(sqrt(1+x)*pi).*(2*x+1)./sqrt(x) - 2*sqrt(1+x).*cos(sqrt(x)*pi).*cos(sqrt(1+x)*pi);
fplot(f,[0 100])
  1 件のコメント
Bruno Luong
Bruno Luong 2024 年 2 月 15 日
編集済み: Bruno Luong 2024 年 2 月 15 日
I believe fplot ezplot replace "*" "/" "^" etc... with elementwise operators ".*" "./', ".^". It interprets as standard math notation.
This how charming or quirking MATLAB is, depending on your taste.

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

カテゴリ

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

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by