Ode45 initial conditions iteration

I am trying to solve the following 2nd order differential equation, which works fine but I need to iterate the initial conditions, so everytime the ode is solved, the last values for q(:,1) and q(:,2) should be the new value for X and Y respectivley.
%solve_2nd_order_diff
duf=@(t,q)[q(2);f*w^2*cos(w*t)-(Cmech/J)*q(2)-(K1/J)*q(1)-(K3/J)*q(1)^3];
for r=1:length(Rad_speed)%freq of harmonic fluctuations, rad/s
[t,q]=ode45(duf,Time,[X Y]);
Max_values=max(q);
plot(t,q(:,1),'k')
xlabel('TIme,s')
ylabel('Relative Displacement, Rad')
grid
end

 Accepted Answer

Like so
...
[X Y] = ...
for r=1:length(Rad_speed)%freq of harmonic fluctuations, rad/s
[t,q]=ode45(duf,Time,[X Y]);
[X Y] = [q(end,1) q(end,2)];
...

5 Comments

Thanks for the swift reply.
it says "Too many output arguments." in regards to the [X Y] = [q(end,1) q(end,2)]; line
Can you upload your full code?
Sorry, I should have written
X = q(end,1); Y = q(end,2);
rather than
[X Y] = [q(end,1) q(end,2)];
Incidentally, your plots will be overwritten unless you use the figure command or hold on (whichever is appropriate.
full code is below, it is working fine now.
J=3.37e-5; %inertia of rotor (kg/m2)
K1=0.7243; %Linear stiffness coef (N.m/rad)
K3=1409.7; %Nonlinear stiffness coef (N.m/rad3)
f=0.005;
Zeta=0.05;%damping ratio
Cmech=2*Zeta*(sqrt(K1/J))*J; %mechanical damping coef
ECF=0.2; %electromagnetic coupling factor
Rint=82;
Celec=ECF^2/2*Rint;%Electrical damping coef
L=160;%Inductance (mH)
X = 0;
Y = 0;
%shaft speed in RPM
Shaft_speed=2000;
%shaft speed in rad/s
Rad_speed=Shaft_speed*((2*pi)/60);
%freq of harmonic fluctuations
w=2*Rad_speed;
w_Hz=w/2*pi; %convert to Hertz
nt=1/w_Hz;
Time=0:nt/100:100*nt;
%solve_2nd_order_diff
duf=@(t,q)[q(2);f*w^2*cos(w*t)-(Cmech/J)*q(2)-(K1/J)*q(1)-(K3/J)*q(1)^3];
Iter=100;
for r=1:Iter
[t,q]=ode45(duf,Time,[X Y]);
X=q(end,1);
Y=q(end,2);
Max_values=max(q)
plot(t,q(:,1),'k')
xlabel('TIme,s')
ylabel('Relative Displacement, Rad')
grid
end
Excellent, but notice that your plot and your Max_values will only apply for the last iteration of the loop (they are overwritten every time through the loop). If that is what you want it is probably best to put that piece of coding outside the loop at the end. To store the Max_values (rather than just have them printed in the command window every iteration), you could use
Max_values(r) = max(q);

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!