Displaying in terms of pi

37 views (last 30 days)
Eric
Eric on 6 May 2015
Commented: Walter Roberson on 6 May 2015
I have the written the following code but I'm not too sure how to display the x values in terms of pi. Is that possible?Also in my current code, it's only showing integer value and not to multiple decimal places despite using format long.
clear;
format long;
fprintf(' x sin(x)');
out = fopen('out.txt','w');% w is used to write data
for i = 0:(pi/11):pi
fprintf(out,'\n %5.0f %5.0f ',i,sin(i));
end;
fclose(out);
OUTPUT:
x sin(x)
0 0
0 0
1 1
1 1
1 1
1 1
2 1
2 1
2 1
3 1
3 0
3 0

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2015
"format long" has no effect on the output of any of the *printf() series. You instructed that no decimal places be used when you requested %5.0f -- the .0 means 0 decimal places.
Notice by the way that you are not writing the header to the file.
If you want to display fraction of pi, then have a look at rats(x/pi)
  2 Comments
Eric
Eric on 6 May 2015
Ahh ofcourse, I've forgotten the basics. Thanks!
The header not being written, why is that?
Walter Roberson
Walter Roberson on 6 May 2015
You used fprintf() with no named output destination for the header, and you do that before you open the output file. The output is going to go to the console. You need to move the header output to after you open the file and you need to designated the output destination in the call, just as you do for writing out the numeric values.

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!