I have to print the values of f(x) = sin(x^3) at the points x = 5.201, −8323.6, 0.0003 in floating point (f format) with 8 digits after the decimal and in scientific notation (e format) with 10 digits after the decimal.
Any help at all would be appreciated. I have no clue how to do this.

 Respuesta aceptada

Star Strider
Star Strider el 10 de Sept. de 2015
Editada: Star Strider el 10 de Sept. de 2015

1 voto

The fprintf function optionally requires a ‘fileID’ variable as its first argument, with 1 indicating ‘stdout’, that being the Command Window. Otherwise it will be to the file you want to write to. (I used it in the first fprintf call but not in the second.)
To print the values of ‘x’ and f(x), this works:
x = [5.201, -8323.6, 0.0003];
f = @(x) sin(x.^3);
fprintf(1, 'f(%f) = %.8f\n', [x; f(x)])
fprintf('f(%f) = %.10e\n', [x; f(x)])
f(5.201000) = 0.63076123
f(-8323.600000) = -0.52794696
f(0.000300) = 0.00000000
f(5.201000) = 6.3076122538e-01
f(-8323.600000) = -5.2794696417e-01
f(0.000300) = 2.7000000000e-11
The (\n) is a newline character.

4 comentarios

Kaylene Widdoes
Kaylene Widdoes el 10 de Sept. de 2015
Awesome! Thank you so much! Any chance you know this one as well? http://www.mathworks.com/matlabcentral/answers/242146-function-file-question
Star Strider
Star Strider el 10 de Sept. de 2015
My pleasure!
Here on MATLAB Answers, if a particular Answer solves your problem, please Accept it.
I looked at your other Question. I don’t understand what x^(+) means, so I’ll wait for you to clarify that.
Kaylene Widdoes
Kaylene Widdoes el 10 de Sept. de 2015
Editada: Kaylene Widdoes el 10 de Sept. de 2015
This was all I was given. Thanks!
Star Strider
Star Strider el 10 de Sept. de 2015
As always, my pleasure!
I posted a Comment to your Question there, rather than an Answer. I don’t understand the Question well enough to be able to provide an Answer.

Iniciar sesión para comentar.

Más respuestas (2)

Stephen23
Stephen23 el 10 de Sept. de 2015
Editada: Stephen23 el 10 de Sept. de 2015

2 votos

This should get you started:
>> x = [5.201,-8323.6,0.0003];
>> sprintf(' %.8f',x)
ans = 5.20100000 -8323.60000000 0.00030000
>> sprintf(' %.10e',x)
ans = 5.2010000000e+000 -8.3236000000e+003 3.0000000000e-004
MATLAB has very readable and accessible documentation complete with working examples for you to try out. This is the best place to find out how to do things and how to use functions. You were told what function to use, the next step is to read its documentation and try some of its examples. Then you can see how these can be adapted to your needs, try some small changes... keep on going until you have figured out the problem. Note you should not try to solve any code problem by sitting down writing code! The most important steps are to first understand the problem and then to understand what tools you have to help you, only then comes writing some code...
Here is the fprintf documentation:
I would also highly recommend that you do these tutorials, which are an excellent introduction to MATLAB:

4 comentarios

Kaylene Widdoes
Kaylene Widdoes el 10 de Sept. de 2015
I'm getting a parse error with 0.0030000?
Steven Lord
Steven Lord el 10 de Sept. de 2015
Don't copy and paste the lines that start with "ans =". Those are the output from the commands on the previous lines (the lines that start with ">>".)
Kaylene Widdoes
Kaylene Widdoes el 10 de Sept. de 2015
So the next step would be to write the sin function correct? So wouldn't it be easiest if I defined a function y=(sin(x^3)) and then did the same sprintf code with y(x) rather than x?
Kaylene Widdoes
Kaylene Widdoes el 10 de Sept. de 2015
Any chance you can give me a start on this problem as well? http://www.mathworks.com/matlabcentral/answers/242146-function-file-question

Iniciar sesión para comentar.

Gayan
Gayan el 30 de Jul. de 2024
X=(5-19/7+2.5^3)^2
X = 320.7937

1 comentario

Walter Roberson
Walter Roberson el 30 de Jul. de 2024
I do not understand how this answer helps people use fprintf() ?

Iniciar sesión para comentar.

Categorías

Preguntada:

el 10 de Sept. de 2015

Comentada:

el 30 de Jul. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by