Index in position 1 exceeds array bounds (must not exceed 1).
Show older comments
I am trying to run a code and it gives an error telling "Index in position 1 exceeds array bounds (must not exceed 1)."
I am trying to solve an equation which has 2 unkown variable, one of which i am assigning an array H=0:5 and trying to solve the other variable using the equation solver code. I have tried this with different equation and got the result however when i try to use the same concept for the equation in the below code it is showing the error. Any guidance or help would be highly appreciate.
the code is below where i am trying to get all values of "th" for every value of "z0".
clear; close all; clc;
g= 9.81; %m/s^2
V1= 10; %m/s
V0= 6*V1; %m/s
tetha1= (pi/6)*(180/pi); %Porjectile inclination angle
x0= 10; %m
y0= 11; %m
hmax=10;
tp=0.1; %s
% The impact time th
syms z0 th;
H=0:5;
t_h=zeros(101,7);
i=1;
for z0 = H
Eq=(V1*th*sind(tetha1)+((1/2)*g*tp*(tp-2*th)))-(sqrt((4*(th-tp)^2*V1^2)-y0^2-(-x0+V1*th*cosd(tetha1))^2))==z0;
P=solve(Eq,th,'IgnoreAnalyticConstraints',true);
T=double(P);
t_h(i,1)=z0;
t_h(i,2)=T(2,1);
i=i+1;
end
fprintf('%2.5f\n',T);
Answers (1)
Ameer Hamza
on 27 Apr 2020
The statement
P=solve(Eq,th,'IgnoreAnalyticConstraints',true);
does not always return a solution. Sometimes it return an empty matrix. Therefore when you run these lines
T=double(P);
t_h(i,1)=z0;
t_h(i,2)=T(2,1);
On last line T(2,1) does not exist and MATLAB throws an error.
4 Comments
mayank Shroff
on 27 Apr 2020
Ameer Hamza
on 27 Apr 2020
The equation in this code is quite simple
eqn = 2408.814* t^2 - 327.20*t + 8.06*t*H + 0.098*H - H^2 - 196 == 0 ;
It is just a quadratic equation which can be solved easily. However, your equation is more complicated, so solve() is not able to find a solution.
mayank Shroff
on 27 Apr 2020
Edited: Ameer Hamza
on 27 Apr 2020
Ameer Hamza
on 27 Apr 2020
This time again, for H=3, the solve() function returns an empty vector. MATLAB symbolic engine is not able to solve the equation for H=3, and it returns an empty vector. Since the symbolic engine is failing on your system of equations, I recommend using fsolve(), which is a numerical solver.
Categories
Find more on Matrix Indexing 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!