ODE45 WITH MATRIX V AND TIME

Hi
I have a function
function xprime = tdofssfun(t,x)
global a b u k x0 T v1
xprime = (a-b*k)*x -(a-b*k)*[ v1;0;0];
end
THEN
[t,x] = ode45('tdofssfun',tspan,x0,options);
and I want solve the equation where a 3*3 matrix, b 3*1 matrix k1*3 matrix
In general i can solve this equation if v1 is constant or has function with time
but the problem I have matrix of value for v1 and evrey value calculating at on time so i want to solve this equation
as example
v1=[0.2 0.5 0.6 0.9]
with time
t=[0.5 1 1.5 2]
Thx

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 7 月 19 日

1 投票

Instead of using global, parameterize your function. Then it becomes easy to loop over an array of values.

5 件のコメント

rami
rami 2012 年 7 月 19 日
The problem is time: The functiom must dolve ODE45 for matrix V1, according to the time matrix, which mean the first value for v1 must solving according time from 0 to first value of time matrix, where second value of v2 must running with time betweem first value and second value for time matrix and so
Walter Roberson
Walter Roberson 2012 年 7 月 19 日
編集済み: Walter Roberson 2012 年 7 月 19 日
function xprime = tdofssfun(t, x, a, b, u, k, x0, T, v)
xprime = (a-b*k)*x - (a-b*k)*[ v;0;0];
end
for N = 1 : length(v1) - 1
[t{N}, x{N}] = ode45(@(t,x) tdofssfun(t, x, a, b, u, k, x0, T, v1(N)), tspan(N:N+1), x0, options);
end
You should also be considering optimization by pre-calculating common expressions.
rami
rami 2012 年 7 月 20 日
編集済み: rami 2012 年 7 月 20 日
I try this, but when I plot it for v consist of 57 value with time from 1 to 57, and after plot it I have result that simulate all value for v ( in my example first value 0 and end 18) with time between 56 to 57. Which mean that calculate all value of v in on one period, and repeat it. While what i need that function solve v(1)for period between 0-1, then v(2) between period 1-2 and so on. then plot result for v according time from 1-57
for plot i use
plot(t{N}, x{N}(:,1))
rami
rami 2012 年 7 月 20 日
編集済み: Walter Roberson 2012 年 7 月 20 日
however this all program:
function xprime = tdofssfun(t, x, a, b, k, x0, v)
xprime = (a-b*k)*x - (a-b*k)*[ v;0;0]
end
a=[0 1 0;0 0 1; -1.9*10^5 -2*10^5 -2.4*10^5];
% define the input matrix, b
b=[0 ;0 ;6*10^5];
% define the output matrix for transient response, c, displacements only
c = [1 0 0];
k=[ 110 16 0];
v=[ 0 0.0245 0.0736 0.1471 0.2452 0.3678 0.5149 0.6865 0.8827 1.1034 1.3486 1.6183 1.9125 2.2313 2.5745 2.9423 3.3346 3.7515 4.1928 4.6587 5.1491 5.6640 6.2034 6.7674 7.3558 7.9688 8.6063 9.2684 9.9304 10.5679 11.1809 11.7693 12.3333 12.8727 13.3876 13.8780 14.3439 14.7852 15.2021 15.5944 15.9622 16.3072 16.6504 16.9692 17.2634 17.5331 17.7783 17.9990 18.1952 18.3668 18.5139 18.6365 18.7346 18.8081 18.8572 18.8817 18.8817];
tspan = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57];
x0 = [0 0 0]'; % initial condition vector, note transpose
options = []; % no options specified for ode45 command
for N = 1 : length(v) - 1
[t{N}, x{N}] = ode45(@(t,x) tdofssfun(t, x, a, b, k, x0, v(N)), tspan(N:N+1), x0, options);
end
figure(1)
plot(t{N}, x{N}(:,1))
Walter Roberson
Walter Roberson 2012 年 7 月 20 日
I would need to run the code to test (which I cannot do this week), but what I suspect you want as output is
tvals = vertcat(t{:});
xvals = vertcat(x{:});
x1vals = xvals(:,1);
plot(tvals, xvals);

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2012 年 7 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by