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 个)

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 个评论

Thank you for your answer. Do i need to rewrite it?
function dydt= test(t,y,k) ode1= k*A(k)+(y(1)-2) %A is the vector
Because i keep getting "Index exceeds array bounds"
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?
size(A) ans= 15 1
k=2
Diego Dessi
Diego Dessi 2018-10-25
编辑:Diego Dessi 2018-10-25
I wrote i have a vector/matrix (15x1),i did not specify that the vector was A. My mistake,sorry
@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?
So, I have a file where A is defined,A is a matrix (15x1). I recall this file in the "test" file so that i can use A. I use the function ODE45.
function dydt=test(t,y,x)
file.m %in which A is defined
ode1= k*A(x)+(y(1)-2) %A is the vector
dydt = [ode1];
end
Then 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
I need a cycle for the ode file,when first entering the ode i want MATLAB to pick A(1,1) and y(1)=y0,the second time A(2,1) and y(1)=the previous y(1) and so on for all the lenght of A,so i want 15 results. Hope i explained it well,at this point i don't know if i have to use the ode or not.
Jan
Jan 2018-10-26
编辑:Jan 2018-10-26
@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.
Here are the 3 files. In the "tempint.m" file i want to do the ode iteration with every slot of my matrix "A".
  1. ode1=(area*A(1))+(hest*(Test-y(1))); with y(1)=y0
  2. ode1=(area*A(2))+(hest*(Test-y(1))); with y(1)=the y(1) of the last step
I tried to do this with a for cycle in the file "Result.m" and i don't know if this is correct and if it is possible to do,i just want to run the ode for 15 times. I am really sorry if i am not making it understandable.

请先登录,再进行评论。

类别

产品

版本

R2018a

标签

提问:

2018-10-24

评论:

2018-10-26

Community Treasure Hunt

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

Start Hunting!

Translated by