How to solve the differential equation say ode45 with multiple inputs(vector)

31 views (last 30 days)
I have a a differential equation swing100
function dydt= swing100(t,y)
global Tm Te D H wB ;
dydt=[wB.*y(2);(0.5/H).*(Tm-Te-(D.*y(2)))];
This is called from a call_swing100.m file:
clc
clear all
global Tm Te D H wB ;
Tm=[1.0];
Te=[0.99];
D=[0];
H=[4];
wB=2*pi*50; %constant
y1=[0];
y2=[0];
y0=[y1; y2]; % intial conditions matrix
%dydt = odefun(t,y);
[t,y] = ode45(@swing100,[0 20],y0);
plot(t,y(:,1),'-',t,y(:,2),'--')
title('Solution of Swing Equation, ');
xlabel('time t');
ylabel('solution y');
legend('y_1','y_2')
This works Fine.
Now I want to solve the same with not one Tm but with say three values of Tm,Te,D,H. That is now Tm etc are vector inputs. So how do I do that? I have tried many 'wrong 'things and keep getting errors.
In simulink for example it works when I give an input vector. How do I do that in .m? Should I have to write a for loop and call each one once?

Accepted Answer

Star Strider
Star Strider on 16 Apr 2014
This works:
swing100 = @(t,y, Tm,Te,D,H,wB) [wB.*y(2); (0.5/H).*(Tm-Te-(D.*y(2)))];
Tm = 1:3:9;
Te = .99*(1:3);
D = (0:2);
H = 4:6;
wB=2*pi*50; %constant
y0 = [0 0];
ts = linspace(0,20,20);
tic
for k1 = 1:3
for k2 = 1:3
for k3 = 1:3
for k4 = 1:3
f = @(t,y) swing100(t,y, Tm(k1),Te(k2),D(k3),H(k4),wB);
[t,y] = ode45(f, ts, y0);
ymtx(k1,k2,k3,k4,:,:) = y;
end
end
end
end
toc
figure(1)
plot(ts, squeeze(ymtx(1,2,3,3,:,:)) )
legend('y_1', 'y_2', 'Location', 'SE')
grid
I specified a range of integration times in variable ts so all the y vectors would have the same number of rows. (The entire set of integrations took about 90 seconds.)
I created the vectors for the variables Tm, Te, D, H you want to iterate over, so you will have to substitute the correct ones for mine.
Plotting it is not straightforward, so I included an example of that in case you want to plot them.

More Answers (1)

Satyanarayan
Satyanarayan on 16 Apr 2014
Thanks Star Rider for your response. I will try and get back on your response. The main point I wanted to ask was: Suppose that we add a second input..(Not related to the first)
Tm=[1 2] Te=[0.99 1.99] D=[ 0 0] H=[4 3]
_wB is constant
The equation syntax is same. I am solving two ODEs parallely. The first one with_
Tm=1 Te=0.99 D=0 H=4.
The second one with
Tm=2 Te=1.99 etc.
And we get y(1) and Y(2) for the first equation and y(1) and y(2) for the second set.
In simulink this is easily done. Is the way you suggested the only way to do it?
  3 Comments
Satyanarayan
Satyanarayan on 16 Apr 2014
OK. Thanks for the answer. Meanwhile I tried what you said and modified a little bit from my requirement point of view. I have marked your response as answer. Thank You.
Star Strider
Star Strider on 16 Apr 2014
My pleasure!
You can use the array reference I used in the plot command to get the values you want from your ODE integrations.
And thanks!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!