What is suitable script for the question?

Untitled.png plot?
what is the difference between 2 script mentioned below for the above question?
Script 1: Script 2:
x=linspace(0,pi,100); x=[0:pi];
>> y=cos(x); y=cos(x);
>> z=1-((x.^2)/2)+((x.^4)/24); z=1-((x.^2)/2)+((x^4)/24);
plot(x,y,x,z) plot(x,y,x,z)

4 件のコメント

Walter Roberson
Walter Roberson 2020 年 2 月 1 日
Use
hold on
between the two plot calls
Manuj Sharma
Manuj Sharma 2020 年 2 月 1 日
Which is correct script for the above question?
Walter Roberson
Walter Roberson 2020 年 2 月 1 日
Looking again, I see that you only have one call to plot. You do not need hold on with the way you plot.
Walter Roberson
Walter Roberson 2020 年 2 月 1 日
As edward pointed out, in script 2 you are missing . before ^4 . That would cause the code to fail.
If that were to be changed, then both scripts would work. However, the first script would plot 100 points starting at 0 and ending at pi, but the second script would plot only at locations [0, 1, 2, 3]

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

回答 (1 件)

edward holt
edward holt 2020 年 2 月 1 日

0 投票

Short answer: script 1.
x = linspace(0,pi,100)
creates a vector with 100 values in between 0 and pi, so when plotting y and z against x you get a nice smooth plot.
x = 0:pi
on the other hand creates just a vector of length 4. [1 2 3 4] Plotting y and z over this will not be smooth.
Also in script 2 there is a missing '.' before '^4'

3 件のコメント

John D'Errico
John D'Errico 2020 年 2 月 1 日
Please, when no effort is made, please try not to just do people's homework for them. You can guide them if they make an effort, but if all you do is their homework, then you make the site into a place where that is expected.
edward holt
edward holt 2020 年 2 月 1 日
My apologies. Thank you for pointing this out to me.
Manuj Sharma
Manuj Sharma 2020 年 2 月 1 日
This is not my assignment or home work. I a beginner in MATLAB and have some issues with understanding the code.

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

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

タグ

質問済み:

2020 年 2 月 1 日

コメント済み:

2020 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by