Index exceeds the number of array elements. Index must not exceed 1.

14 Ansichten (letzte 30 Tage)
Alina Abdikadyr
Alina Abdikadyr am 8 Feb. 2023
Kommentiert: Les Beckham am 8 Feb. 2023
Could you please help me to solve the problem. I'm trying to calculate the difference between heights in a loop.
But it gives error "Index exceeds the number of array elements. Index must not exceed 1."
My code is
clear all; close all
W = 10000;
S = 40;
AR = 7;
cd0 = 0.005;
k = 1 / pi / AR;
j=0;
hv=6:-.01:0;
figure(1);hold on; xlabel('h');ylabel('V')
for h1=6:-.01:0
j=j+1;
cdminp=4*cd0;
rho1(j)=1.225*exp(-h1/10.4);
clminp=sqrt(3*cd0/k);
Vminp(j)=sqrt(2*W/rho1(j)/S/clminp);
dh(j)=h1(j)-h1(j+1);
end
figure(1); plot(hv, Vminp);
set(gca, 'XDir','reverse');
  4 Kommentare
Alina Abdikadyr
Alina Abdikadyr am 8 Feb. 2023
Thank you! I want to calculate the difference between the first element and the folloing elements.
If my h1=6:-.01:0
Probably I should write as:
dh=h1(1)-h1(j+1);
so I need to get the values of dh = 0.01;0.02;0.03;0.04 and so on
Les Beckham
Les Beckham am 8 Feb. 2023
If you know what the h1 vector is already, you don't need a loop to calculate the accumulated difference between elements of it.
h1=6:-.01:0;
dh = -cumsum(diff(h1));
dh(1:10) % look at the first ten elements
ans = 1×10
0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Dyuman Joshi
Dyuman Joshi am 8 Feb. 2023
You can do that without the loop
W = 10000;
S = 40;
AR = 7;
cd0 = 0.005;
k = 1 / pi / AR;
hv = 6:-.01:0;
cdminp = 4*cd0;
clminp = sqrt(3*cd0/k);
rho1 = 1.225.*exp(-hv./10.4);
Vminp = (2.*W./rho1./S).^(1/2);
dh = hv(1)-hv(2:end)
dh = 1×600
0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900 0.3000
figure(1);
hold on;
xlabel('h');ylabel('V')
plot(hv,Vminp)
set(gca, 'XDir','reverse');

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by