How to fix: Index Exceeds Matrix Dimensions
1 次查看(过去 30 天)
显示 更早的评论
err: Index Exceeds Matrix Dimensions
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!