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

조회 수: 2 (최근 30일)
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

채택된 답변

the cyclist
the cyclist 2020년 12월 25일
편집: the cyclist 2020년 12월 25일
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.
  댓글 수: 2
Ahmad Magableh
Ahmad Magableh 2020년 12월 25일
i have tried this command but it doesn't working , could you please edit my code with the new comand ,
the cyclist
the cyclist 2020년 12월 26일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by