How to solve the error and get p2 value?

1 view (last 30 days)
when i Run this code got
Error: Index exceeds matrix dimensions.
please anyone tell me my mistakes....and How to get p2 value....
Thanks...

Accepted Answer

Mathieu NOE
Mathieu NOE on 30 Aug 2021
hello
your errors are due to trying to access a structure in a wrong way , like :
m1=xa(end,2);
I suppose the intention is to access the y output in this structure
so it should be :
m1=xa.y(end,2);
but this is the second sample of the simulation, and I guess what you wanted is to have the last sample , 2nd row of the y array
xa = struct with fields:
solver: 'ode45'
extdata: [1×1 struct]
x: [1×889 double]
y: [6×889 double]
stats: [1×1 struct]
idata: [1×1 struct]
so I suppose this is the correct output :
m1=xa.y(2,end);
but I am surprised to see very big numbers , and also the plot is a bit strange with only a straight line , so I wonder where we still have trouble with your code.
I also added "hold on" after your plot command as I guess you want to have all points of your loop being plotted
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
xa=ode45(f,[0 10],[0 1 p1 0 0.3 0]);
% ylim([0 5]);
%plot(t,x(:,3),'--')
% m1=xa(end,2); % your code
% m1=xa.y(end,2); % correction # 1
m1=xa.y(2,end); % my guess ?
%.........................................
xa=ode45(f,[0 10],[0 1 p2 0 0.3 0]);
% m2=xa(end,3);
% m2=xa.y(end,3); % correction # 1
m2=xa.y(3,end); % my guess
plot([p1 p2],[m1 m2]);hold on
else
p2=(p2-p1)/(m2-m1)*(1.0-m1)+p1;
xa=ode45(f,[0 10],[0 1 p2 0 0.3 0]);
% m2=xa(end,2); % your code
% m2=xa.y(end,2); % correction # 1
m2=xa.y(2,end); % my guess
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

Tags

Community Treasure Hunt

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

Start Hunting!