i couldn't understand the problem in it
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
SAHIL SAHOO
am 28 Sep. 2022
Kommentiert: Walter Roberson
am 28 Sep. 2022
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);
subplot 211
plot(T./tp,Y(:,9));
xlabel("time with respect to tp")
ylabel("phase difference")
grid on
subplot 212
plot(T./tp,Y(:,5));
ylim([0 60]);
xlim([0 1.5E5]);
xlabel("time with respect to tp")
ylabel("amplitude")
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));
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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)
0 Kommentare
Weitere Antworten (1)
vamshi sai yele
am 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138775/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138780/image.png)
Snipet-1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138785/image.png)
Snipet-2
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138790/image.png)
Snipet-3
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 Kommentar
Walter Roberson
am 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
Siehe auch
Kategorien
Mehr zu Linear Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!