Increment a vector in ODE45
显示 更早的评论
Hi everyone. I have to solve an equation using ODE45,in which i have a vector (15x1). How can i increment the vector position? I have: (the first one is an example)
function dydt= test(t,y,x)
ode1= k*A(x)+(y(1)-2) %A is the vector
end
in another file i have
tspan = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
y0 = [20];
for x=1:1:15
[t,y] = ode45(@(t,y) test(t,y,x),tspan,y0);
end
The problem is that i want A(1) in the first iteration where y(1) is y(0),A(2) where y(1) is the result of the last step...till A(15). How can i do it? I am sorry for my bad explanation and for my bad english. Thanks to everyone.
回答(1 个)
Jan
2018-10-24
tspan = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
y0 = 20;
tC = [];
yC = [];
for k = 1:14
[t,y] = ode45(@(t,y) test(t,y,k), tspan(k:k+1), y0);
tC = cat(1, tC, t); % Collect the total output
yC = cat(1, yC, y);
y0 = y(end); % Set new initial value
end
9 个评论
Diego Dessi
2018-10-24
Diego Dessi
2018-10-24
Jan
2018-10-25
You did not post the definition of A. I guess, that the error is caused by A(k). You can check this easily using the debugger. Type this in the command window:
dbstop if error
Then run the code again until Matlab stops. Now check the sizes:
size(A)
k
What do you see?
Diego Dessi
2018-10-25
Diego Dessi
2018-10-25
编辑:Diego Dessi
2018-10-25
Jan
2018-10-25
@Diego Dessi: The problem is still not clear. Is this the failing code:
function dydt= test(t,y,k)
ode1= k*A(k)+(y(1)-2) %A is the vector
? Then where is A defined? Where do you create dydt? What do you do with ode1?
Diego Dessi
2018-10-25
编辑:Diego Dessi
2018-10-25
@Diego: The shown code fails, because "file.m" considers "file" to be a struct with the field "m". Please post the real code. If you call a function or script instead of "file.m", it should work. So if you still do have any problems, please post the real code and a copy of the error message.
I've posted already some code, which let the integration run piecewise in the specified intervals of tspan. Your code calls the integration 15 times and overwrite the result in each iteration. I do not understand, what this code should achieve.
Diego Dessi
2018-10-26
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!