Displaying in terms of pi

조회 수: 37 (최근 30일)
Eric
Eric 2015년 5월 6일
댓글: Walter Roberson 2015년 5월 6일
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

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 6일
"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
Eric
Eric 2015년 5월 6일
Ahh ofcourse, I've forgotten the basics. Thanks!
The header not being written, why is that?
Walter Roberson
Walter Roberson 2015년 5월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by