MATLAB Answers

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

24 views (last 30 days)
clear all
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


Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 25 Dec 2020
Edited: the cyclist on 25 Dec 2020
You can use the format command, e.g.
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.


the cyclist
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by