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!