Question:
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2π with steps of π/16. The numbers should have three digits to the right of the decimal. Determine an appropriate field width. Use fprintf to display the output in tabular format with the following headers.
x sin(x) cos(x) tan(x)
My code:
X = [0:(pi/16):(2*pi)]
a = sin(X);
b = cos(X);
c = tan(X);
fprintf('%6s %6s %6s\n','sin','cos','tan');
fprintf('%.3f \t %.3f \t %.3f\n',a,b,c);
and my output:
sin cos tan
0.000 0.195 0.383
0.556 0.707 0.831
0.924 0.981 1.000
0.981 0.924 0.831
0.707 0.556 0.383
0.195 0.000 -0.195
-0.383 -0.556 -0.707
-0.831 -0.924 -0.981
-1.000 -0.981 -0.924
-0.831 -0.707 -0.556
-0.383 -0.195 -0.000
1.000 0.981 0.924
0.831 0.707 0.556
0.383 0.195 0.000
-0.195 -0.383 -0.556
-0.707 -0.831 -0.924
-0.981 -1.000 -0.981
-0.924 -0.831 -0.707
-0.556 -0.383 -0.195
-0.000 0.195 0.383
0.556 0.707 0.831
0.924 0.981 1.000
0.000 0.199 0.414
0.668 1.000 1.497
2.414 5.027 16331239353195370.000
-5.027 -2.414 -1.497
-1.000 -0.668 -0.414
-0.199 -0.000 0.199
0.414 0.668 1.000
1.497 2.414 5.027
5443746451065123.000 -5.027 -2.414
-1.497 -1.000 -0.668
-0.414 -0.199 -0.000

2 件のコメント

Stephen23
Stephen23 2018 年 2 月 3 日
編集済み: Stephen23 2018 年 2 月 3 日
@Michael spillane: please do not post the same question multiple times (I closed the other ones).
What is your question? You showed some code but did not ask us anything.
Michael spillane
Michael spillane 2018 年 2 月 3 日
編集済み: Michael spillane 2018 年 2 月 3 日
Yea it didnt appear on my activity feed so I posted it again.
The question was in the thread title.. regardless,
The question I have is that the 3rd to last sin value and the 9th to last tan value are extremely wrong, so i am trying to figure out why my output is as such, as I believe that I have coded it correctly so debugging is difficult to spot my own mistake

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

 採用された回答

Stephen23
Stephen23 2018 年 2 月 3 日
編集済み: Stephen23 2018 年 2 月 3 日

0 投票

You forgot that MATLAB is column-major:
X = 0:pi/16:2*pi; % square brackets are NOT required.
a = sin(X);
b = cos(X);
c = tan(X);
fprintf('%8s%8s%23s\n','sin','cos','tan');
fprintf('%8.3f%8.3f%23.3f\n',[a;b;c]); % note ; not ,

4 件のコメント

Michael spillane
Michael spillane 2018 年 2 月 4 日
It is still outputting the super huge numbers for the 3rd to end of sin and 9th to end of tan. Maybe it is an issue with the software because I dont see why it would output that giant value
per isakson
per isakson 2018 年 2 月 4 日
Looks ok to me
>> tan(pi/2)
ans =
1.6331e+16
Michael spillane
Michael spillane 2018 年 2 月 4 日
ah.. when i had the table improperly spaced it appeared as if the large # was in the sin column. Im assuming the tanpi/2 is near the asymptote where the graph goes off to another universe
Stephen23
Stephen23 2018 年 2 月 4 日
@Michael spillane: that is why I increased the width of the tan column to 23.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by