Displaying in terms of pi
조회 수: 37 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
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 Center 및 File 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!