How to remove exponential answers

8 visualizaciones (últimos 30 días)
Dominic
Dominic el 10 de Feb. de 2024
Comentada: Stephen23 el 10 de Feb. de 2024
Dear all,
Im new to Matlab, how can I remove exponetial answers in my code, the code is for successive substitution, i would like to be able to remove the exponential from x and relative error displayed by fprintf. I have tried using format shortG ath the begining of the script and it doesn't work.
All help appreciated.
for i=1:1000000
x=f(x);
error=abs((x-f(x))/x);
if error<relerror
break
end
end
relerrorpercent = error*100;
d=(x^3 -3*x +1);
answer=round(d);
X=round(x,errorin);
fprintf('\n x=%i \n',x)
fprintf(' relative error = %i percent \n',relerrorpercent)
x=3.471066e-01
relative error = 4.807316e-02 percent
  1 comentario
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 10 de Feb. de 2024
If understood your question correctly, why not to use a logical indexing to sort out error.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 10 de Feb. de 2024
Editada: Matt J el 10 de Feb. de 2024
x=3.471066e-01;
relerrorpercent= 4.807316e-02;
fprintf('\n x=%.5f \n',x)
x=0.34711
fprintf(' relative error = %.5f percent \n',relerrorpercent)
relative error = 0.04807 percent
  6 comentarios
Dominic
Dominic el 10 de Feb. de 2024
oh okay, thanks so much.
Stephen23
Stephen23 el 10 de Feb. de 2024
Note that you do not need to construct the format string, you can simply provide the required number of decimals as an input to FPRINTF:
d = 4;
fprintf("x = %0.*f",d,3.471066e-01)
x = 0.3471

Iniciar sesión para comentar.

Más respuestas (1)

Hassaan
Hassaan el 10 de Feb. de 2024
Editada: Hassaan el 10 de Feb. de 2024
Of what I understand about the problem:
Assuming x and error are the values you want to print in a fixed-point format without exponential notation, and you want to maintain a certain precision for x and display the relative error as an integer percentage, you can do something like this:
% Assuming the rest of your code is correct and just focusing on the display part:
relerrorpercent = error * 100;
d = (x^3 - 3*x + 1);
answer = round(d);
% If you want to control the number of digits after the decimal for `x`, you can specify it like so:
fprintf('\n x=%.4f \n', x); % Adjust the '4' to however many digits you want
% For relative error as an integer percentage, it's okay as is but you might want to ensure it's always an integer:
fprintf(' relative error = %0.0f percent \n', relerrorpercent);
In the provided fprintf for x, %.4f specifies that x should be printed with four digits after the decimal point. You can adjust the 4 to any number of digits you prefer. For the relative error, %0.0f ensures that it is rounded to the nearest integer when displayed.
Remember, the choice of precision (%.4f in this example) can be adjusted based on how many digits you find meaningful for your calculations or for the display of results.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  4 comentarios
Dominic
Dominic el 10 de Feb. de 2024
Editada: Dominic el 10 de Feb. de 2024
could you explain how this code works? like what does the %%0.%df mean?
sprintf('%%0.%df', numDecimals);
Hassaan
Hassaan el 10 de Feb. de 2024
% Ask the user for the number of decimal places
numDecimals = input('Enter the number of decimal places: ');
% Construct the format specifier string dynamically
formatSpecifier = sprintf('%%0.%df', numDecimals);
% Example variable to print
x = 3.141592653589793;
% Print the variable with the user-specified number of decimal places
fprintf(['\n x = ', formatSpecifier, '\n'], x);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Iniciar sesión para comentar.

Categorías

Más información sobre Oil, Gas & Petrochemical en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by