How to remove exponential answers

8 visualizzazioni (ultimi 30 giorni)
Dominic
Dominic il 10 Feb 2024
Commentato: Stephen23 il 10 Feb 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 Commento
Sulaymon Eshkabilov
Sulaymon Eshkabilov il 10 Feb 2024
If understood your question correctly, why not to use a logical indexing to sort out error.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 10 Feb 2024
Modificato: Matt J il 10 Feb 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 Commenti
Dominic
Dominic il 10 Feb 2024
oh okay, thanks so much.
Stephen23
Stephen23 il 10 Feb 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

Accedi per commentare.

Più risposte (1)

Hassaan
Hassaan il 10 Feb 2024
Modificato: Hassaan il 10 Feb 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 Commenti
Dominic
Dominic il 10 Feb 2024
Modificato: Dominic il 10 Feb 2024
could you explain how this code works? like what does the %%0.%df mean?
sprintf('%%0.%df', numDecimals);
Hassaan
Hassaan il 10 Feb 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.

Accedi per commentare.

Categorie

Scopri di più su Oil, Gas & Petrochemical in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by