Displaying in terms of pi

37 visualizaciones (últimos 30 días)
Eric
Eric el 6 de Mayo de 2015
Comentada: Walter Roberson el 6 de Mayo de 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

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Mayo de 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 comentarios
Eric
Eric el 6 de Mayo de 2015
Ahh ofcourse, I've forgotten the basics. Thanks!
The header not being written, why is that?
Walter Roberson
Walter Roberson el 6 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by