How to fix: Index Exceeds Matrix Dimensions

1 view (last 30 days)
err: Index Exceeds Matrix Dimensions

Accepted Answer

Wan Ji
Wan Ji on 29 Aug 2021
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

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!