truncate at 10^-3
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
i have this code i want to truncate the solution at 10^-3 at each iteraion can anyone help me
t0=0;
y0=1;
tn=1;
h=0.1;
t=[t0:h:tn];
f=@(t,y)(1+t-y)
f1=@(t,y)1
f2=@(t,y)-1
n=length(t);
y_t=zeros(1,n);
y_t(1)=y0;
for i=1:n-1
y_t(i+1)=y_t(i)+h*f(t(i),y_t(i))+(h^2/2)*(f1(t(i),y_t(i))+f2(t(i),y_t(i))*f(t(i),y_t(i)));
end
0 Kommentare
Antworten (1)
Matt J
am 17 Mär. 2023
Bearbeitet: Matt J
am 17 Mär. 2023
A few options:
x=round(pi,3)
y=floor(pi*1000)/1000
sprintf('%.3f',x)
sprintf('%.3f',y)
4 Kommentare
Matt J
am 17 Mär. 2023
I imagine you would want to do this:
y_t(1)=y0;
for i=1:n-1
y_t(i+1)=y_t(i)+h*f(t(i),y_t(i))+h^2/2*(f1(t(i),y_t(i))+f2(t(i),y_t(i))*f(t(i),y_t(i)))
end
y_tt=fix(y_t*1e3)/1e3;
M=[y_tt',y_t']
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!