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
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 Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2018년 2월 3일

댓글:

2018년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by