How to remove exponential answers

8 次查看(过去 30 天)
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 个评论
Sulaymon Eshkabilov
If understood your question correctly, why not to use a logical indexing to sort out error.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2024-2-10
编辑:Matt J 2024-2-10
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 个评论
Stephen23
Stephen23 2024-2-10
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

请先登录,再进行评论。

更多回答(1 个)

Hassaan
Hassaan 2024-2-10
编辑:Hassaan 2024-2-10
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 个评论
Dominic
Dominic 2024-2-10
编辑:Dominic 2024-2-10
could you explain how this code works? like what does the %%0.%df mean?
sprintf('%%0.%df', numDecimals);
Hassaan
Hassaan 2024-2-10
% 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Oil, Gas & Petrochemical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by