i couldn't understand the problem in it

1 view (last 30 days)
I think the code is correct but some lsit mistake please help me to find it.
ti = 0;
tf = 1E-3;
tspan=[ti tf];
y0 = [0.8;0.7;0.9;0.1;0.4;0.2;4.17;3.17;5.17]; % intial conditions
tp = 5.4E-9;
o = sort(10e3*rand(1,3),'ascend'); %detuning frequency
[T,Y]= ode45(@(t,y) rate_eq(t,y,o),tspan,y0);
Error using odearguments
@(T,Y)RATE_EQ(T,Y,O) returns a vector of length 12, but the length of initial conditions vector is 9. The vector returned by @(T,Y)RATE_EQ(T,Y,O) and the initial conditions vector must have the same
number of elements.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
subplot 211
xlabel("time with respect to tp")
ylabel("phase difference")
grid on
subplot 212
ylim([0 60]);
xlim([0 1.5E5]);
xlabel("time with respect to tp")
grid on
function dy = rate_eq(t,y,o)
dy = zeros(12,1);
P = 1.27;
a = 0.1;
tc = 230E-6;
tp = 5.4E-9;
k = 1E-3;
dy(1) = (P - y(1).*((abs(y(4)))^2 +1))./tc;
dy(2) = (P - y(2).*((abs(y(5)))^2 +1))./tc;
dy(3) = (P - y(3).*((abs(y(6)))^2 +1))./tc;
dy(4) = (y(1) - a).*(y(4)./tp) + (k./tp).*(y(5)).*(cos(y(7))) + (k./tp).*(y(6)).*(cos(y(9)));
dy(5) = (y(2) - a).*(y(5)./tp) + (k./tp).*(y(4)).*(cos(y(7))) + (k./tp).*(y(6)).*(cos(y(8)));
dy(6) = (y(3) - a).*(y(6)./tp) + (k./tp).*(y(4)).*(cos(y(9))) + (k./tp).*(y(5)).*(cos(y(8)));
dy(7) = o(1,1) - (k./tp).*(y(4)./y(5) + y(5)./y(4)).*sin(y(7))+ (k./tp).*(y(6)./y(4)).*sin(y(9)) + (k./tp).*(y(6)./y(5)).*sin(y(8));
dy(8) = o(1,2) - (k./tp).*(y(5)./y(6) + y(6)./y(5)).*sin(y(8))+ (k./tp).*(y(4)./y(6)).*sin(y(9)) + (k./tp).*(y(4)./y(5)).*sin(y(7));
dy(9) = o(1,3) - (k./tp).*(y(6)./y(4) + y(4)./y(6)).*sin(y(9))+ (k./tp).*(y(5)./y(4)).*sin(y(7)) + (k./tp).*(y(5)./y(6)).*sin(y(8));

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2022
dy = zeros(12,1);
You initialize 12 return values but you then only store up to dy(9). You only need zeros(9,1)

More Answers (1)

vamshi sai yele
vamshi sai yele on 28 Sep 2022
Hi Sahil,
I understand that you are facing issue with the written code. I have tried running the given code from my end and I found the error.
It says that vector must have the same number of elements”, but the initial conditions mentioned in the code and the returning vector has different length’s.
I added three more points in the initial condition randomly just to check the behavior of the code and it did work.
Here are few snippets that I have tried as an initial condition and it’s respective output figures.
From the above attached snipets it can be found that once the length of the initial condition is increassed to 12, this code works simply perfect and gives us the expected output figure without any errors.
Hence, I would recommend to include three more initial conditions which describes the exact behavior of the model and code runs without any hicupps.
Hope you find it helpful!
Happy Coding!
  1 Comment
Walter Roberson
Walter Roberson on 28 Sep 2022
zeros are assigned to entries 10, 11, 12, so they are always going to integrate to 0. There is no point in using those. Just assign zeros(9,1) instead

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!