i couldn't understand the problem in it
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2022년 9월 28일
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
추가 답변 (1개)
vamshi sai yele
2022년 9월 28일
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.
Snipet-1
Snipet-2
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
Walter Roberson
2022년 9월 28일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!