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
댓글 수: 0
채택된 답변
the cyclist
2020년 12월 25일
편집: the cyclist
2020년 12월 25일
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
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!