i am a beginner matlab user , and i have problem with the number of decimal places , i need to increase them , what should i do ? here is the code , can anyone just edit it to increase the number of decimal places
3 views (last 30 days)
Show older comments
Ahmad Magableh
on 25 Dec 2020
Commented: the cyclist
on 26 Dec 2020
clear all
clc
f=@(x,y)x+y; %Write your f(x,y) function, where dy/dx=f(x,y), x(x0)=y0.
x0=input('\n Enter initial value of x i.e. x0: '); %example x0=0
y0=input('\n Enter initial value of y i.e. y0: '); %example y0=0.5
xn=input('\n Enter the final value of x: ');% where we need to find the value of y
%example x=2
h=input('\n Enter the step length h: '); %example h=0.2
%Formula: y1=y0+h/2*[f(x0,y0)+f(x1,y1*)] where y1*=y0+h*f(x0,y0);
fprintf('\n x y ');
while x0<=xn
fprintf('\n%4.3f %4.3f ',x0,y0);%values of x and y
k=y0+h*f(x0,y0);
x1=x0+h;
y1=y0+h/2*(f(x0,y0)+f(x1,k));
x0=x1;
y0=y1;
end
0 Comments
Accepted Answer
the cyclist
on 25 Dec 2020
Edited: the cyclist
on 25 Dec 2020
format long
Note that MATLAB always stores the result as double precision by default -- you are only changing how the values will be displayed in the command window.
2 Comments
the cyclist
on 26 Dec 2020
Sorry, I missed that you were directly controlling the significant figures in your output commands. Instead of
fprintf('\n%4.3f %4.3f ',x0,y0);%values of x and y
try something like
fprintf('\n%4.3f %15.8f ',x0,y0);%values of x and y
The string in the fprintf command controls the precision. See the documentation for fprintf for details.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!