# Index exceeds the number of array elements (1)

조회 수: 1(최근 30일)
Holly Martin 27 Nov 2019
Edited: Fangjun Jiang 27 Nov 2019
Hi,
Im doing an assignment where my script should repeats forward euler for different step sizes h between 0.1V/g and 0.001V/g, doubling the number of steps taken each time,and making sure the final time is exactly 3V/g.
function [z,v,studentid] = particle(m,g,k,p,V,h,N)
Above is the function I use for forward euler, and below is the code in another script where im trying to change the step size.
end
%Repeating for different h and N
[z2,v2,~]=particle(5,9.4,0.1,1.2,30,0.1*V/g,10);
[z3,v3,~]=particle(5,9.4,0.1,1.2,30,0.075*V/g,20);
[z4,v4,~]=particle(5,9.4,0.1,1.2,30,0.05*V/g,40);
[z5,v5,~]=particle(5,9.4,0.1,1.2,30,0.025*V/g,80);
[z6,v6,~]=particle(5,9.4,0.1,1.2,30,0.001*V/g,160);
%error
for x=[2 6]
errorz2=z(x)-z;
errorv2=v(x)-v;
end
I feel like ive done it a completely harder way than I need to and wondered if I could simplify it somehow. The assignment also asks me to plot two clearly labelled graphs (using the subplot command), of the magnitude of the error versus h, on log-log axes.
The error for each line is just z(x)-z as it says below, but when i try to run that it says 'Index exceeds the number of array elements (1)'
If someone could please help me that would be so so amazing, I have hit a wall with this.
Thanks :)

로그인 to comment.

### 답변 수 (1)

Fangjun Jiang 27 Nov 2019
Fangjun Jiang 님이 편집함. 27 Nov 2019
You need to replace z with z(1), z2 with z(2), z6 with z(6). Similarly for v, v2, ... v6.
Some fundermental MATLAB basics:
z=rand(1,6) creates a 1x6 vector, you can refer the element, z(1), z(2), ... z(6)
while z1, z2, ... z6 are totally different variables.

#### 댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by