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 Center および File ExchangeScope 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