How to fix: Index Exceeds Matrix Dimensions

조회 수: 1 (최근 30일)
Priya M
Priya M 2021년 8월 29일
댓글: Priya M 2021년 8월 30일
err: Index Exceeds Matrix Dimensions

채택된 답변

Wan Ji
Wan Ji 2021년 8월 29일
Hi, friend
ode45 function outputs a struct with size 1x1 when only one argument is for output data. If you want to output x solution as n*m double matrix use
[t,x] = ode45(...);
Or use
[~,x] = ode45(...);
instead of directly using
x = ode45.....
SO, I modified your code as
clc;
clear all;
t=linspace(0,1,10);
M=0.5;
beta=0.5;
p1=0.1;
p2=0.5;
f=@(t,x) [x(2);x(3);2*x(2).^2+2*(x(2)*x(5))-x(1)*x(3)-x(3)*x(4)+M.^2*x(2)/(1+1./beta);
x(5);x(6);2*x(5).^2+2*(x(2)*x(5))-x(1)*x(6)-x(4)*x(6)+M.^2*x(5)/(1+1./beta)];
err_threshold=1e-3;
iterator =1;
err=1;
while err > err_threshold
if iterator == 1
[~,x]=ode45(f,[0 1],[0 1 p1 0 0.3 0],t);
% ylim([0 5]);
%plot(t,x(:,3),'--')
m1=x(end,3);
%.........................................
[~,x]=ode45(f,[0 1],[0 1 p2 0 0.3 0],t);
m2=x(end,3);
plot([p1 p2],[m1 m2]);
else
p2=(p2-p1)/(m2-m1)*(1.0-m1)+p1;
[~,x]=ode45(f,[0 1],[0 1 p2 0 0.3 0],t);
m2=x(end,3);
plot([p2,m2]);
err=abs(1-m2);
end
iterator=iterator+1;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by