1 view (last 30 days)

my code

b=input('enter the starting value of b here ')

a=input('enter the starting value of a here ')

h= 15

i=600

x(1)= 0

y(1)= 0

for n=1:h:i

x(n+1)=x(n)+(a(400-x(n)))/(sqr((400-x(n))^2+(y(n)^2)))*h

y(n+1)=y(n)+(b-((a*y(n))/(sqr((400-x(n))^2+(y(n)^2)))))*h

if x <= 400

break

end

end

plot(x,y,'r')

Fabio Freschi
on 17 Nov 2019

Checking the sintax of the code only I see 3 issues

1) In the for loop you wrote a(400-x(n)), that is you are addressing the position 400-x(n) = 400 when n = 1 of the variable a. but a is a scalar. so I guess you forgot an operator like, for example a*(400-x(n)) or a+(400-x(n))...

2) you are using sqr instead of sqrt to extract the square root

3) you should preallocate your vectors x and y as x = zeros(i,1); y = zeros(i,1);

Note also that using i as variable can be misleading because it is often used as index

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/491563-euler-method-error-in-code-index-exceeds-the-number-of-array-elements-1-in-my-my-code#comment_768208

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/491563-euler-method-error-in-code-index-exceeds-the-number-of-array-elements-1-in-my-my-code#comment_768208

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/491563-euler-method-error-in-code-index-exceeds-the-number-of-array-elements-1-in-my-my-code#comment_768212

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/491563-euler-method-error-in-code-index-exceeds-the-number-of-array-elements-1-in-my-my-code#comment_768212

Sign in to comment.