Something is wrong with my output...

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 Comments

Stephen23
Stephen23 on 3 Feb 2018
Edited: Stephen23 on 3 Feb 2018
@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.
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

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 3 Feb 2018
Edited: Stephen23 on 3 Feb 2018
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 Comments

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
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
@Michael spillane: that is why I increased the width of the tan column to 23.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!