How to solve the differential equation say ode45 with multiple inputs(vector)
32 views (last 30 days)
Show older comments
Satyanarayan
on 16 Apr 2014
Commented: Star Strider
on 16 Apr 2014
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?
0 Comments
Accepted Answer
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.
0 Comments
More Answers (1)
Satyanarayan
on 16 Apr 2014
3 Comments
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!
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!