Return the final x for different t

1 次查看(过去 30 天)
I tried to create Runge Kutta 4 order method with 2 matlab files:
1) function [fv]=evalfunc(t,x)
fv=4.0*exp(0.8*t)-x/2.0;
2) close all clear all
a=0;
b=4;
N=25;
h=(b-a)/N;
t=[a:h:b];
x(1)=2;
for i=1:N
k1 = h*evalfunc(t(i),x(i));
k2 = h*evalfunc(t(i)+h/2,x(i)+k1/2);
k3 = h*evalfunc(t(i)+h/2,x(i)+k2/2);
k4 = h*evalfunc(t(i)+h,x(i)+k3);
x(i+1) =x(i) + (k1 + 2*k2 + 2*k3 + k4)/6;
t(i+1) = a + h*i;
end
A= [t,x];
plot (t,x)
I want to return the result of x(i+1) for every t. Do I just type [t,x]?
How to get t,x result in column of vector ? because when i run the matlab it shown column 1...34 in command window.

采纳的回答

John Petersen
John Petersen 2012-8-6
Matlab defaults to rows. So when creating a vector on the fly like you have, specify both row and column. These lines should be
x(i+1,1) =x(i) + (k1 + 2*k2 + 2*k3 + k4)/6;
t(i+1,1) = a + h*i;
to give you columnwise vectors. Alternatively, you could reassign them
x=x(:);
t=t(:);
at the end of the function.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by