Index in position 1 exceeds array bounds (must not exceed 1).

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)

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

thanks for your response .
I have tried a code and its running in that. the code in which i have used similar style is as follows
clear; close all; clc;
% assignment 1 task [B]
% given data
clc; clear; close all;
g = 9.81; %m/s^2
V1 = 10; %m/s
V0 = 6*V1;
tetha1 = pi/6; %rad
x0 = 10; %m
y0 = 11; %m
%-----projectile hit target at time t_1----
t1 = 2*V1*sin(tetha1)/g;
fprintf('t_1 = %7.9f sec\n',t1)
%-----time lag to shoot bullet---------
tp = 0.1;
fprintf('t_p = %2.2f sec\n', tp)
%-----time to hit target--------
h_sol1 = zeros(101,7);
syms t H;
i=1;
for H = 0:0.05:5
eqn = 2408.814* t^2 - 327.20*t + 8.06*t*H + 0.098*H - H^2 - 196 == 0 ;
h_sol1(i,1) = H; %H
sol1 = solve(eqn, t); %t time to hit the missile
h_sol1(i,2) = sol1(2,1);
i = i+1;
end
%fprintf('hit time = %5.5f sec', th)
f1 = figure;
figure(f1)
plot(h_sol1(:,1), h_sol1(:,2), 'LineWidth', 1, 'color', 'r')
xlabel('H(meter)')
ylabel('time to hit Projectile (sec)')
grid on;
title('H vs t')
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.
I have tried a different approach. I was able to get values till H=2 but when it goes to H=3 it shows error as below
%%----- Error------%%
Unable to perform assignment because the left and right sides have a different number of
elements.
Error in sym/privsubsasgn (line 1126)
L_tilde2 =
builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 963)
C = privsubsasgn(L,R,inds{:});
Error in Part_b (line 14)
p(i)=solve(E(i),t);
%%---- Error----%%
I was able to get the values for t from the equation till H=2; the code i used is as below
clear; close all; clc;
g = 9.81; %m/s^2
V1 = 10; %m/s
V0 = 6*V1;
tetha1 = 30; %rad
x0 = 10; %m
y0 = 11; %m
tp=0.1;
syms t H
for H=[0;1;2;3]
E=H+(sqrt((4*(t-tp)^2*V1^2)-y0^2-(-x0+V1*t*cosd(tetha1))^2))-((V1*t*sind(tetha1))+((1/2)*g*tp*(tp-2*t)));
end
for i=1:1:4
p(i)=solve(E(i),t);
s(i)=double(p(i));
end
plot(H,s(1,:),'Marker','x','Markersize',5,'color','r'); grid on; axis([0 2 0.6 max(s)+0.1]);
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.

Sign in to comment.

Products

Release

R2019b

Asked:

on 27 Apr 2020

Commented:

on 27 Apr 2020

Community Treasure Hunt

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

Start Hunting!