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 commentaires

Stephen23
Stephen23 le 3 Fév 2018
Modifié(e) : Stephen23 le 3 Fév 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.
Michael spillane
Michael spillane le 3 Fév 2018
Modifié(e) : Michael spillane le 3 Fév 2018
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

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 3 Fév 2018
Modifié(e) : Stephen23 le 3 Fév 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 commentaires

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by