Asked by Maureen Domtche
on 11 Apr 2019

Here's my code and im tryna figure out why i always get "index in position 2 exceed arra bounds"

What do i have to change?

function [t, y] = Euler_implicite(fonction, J ,t0,y0, h,e, Nt)

y(1,:) = y0;

t=zeros(Nt);

t(1) = t0;

for i = 1:Nt

g= y(:,i+1)-y(:,i)- h*fonction;

t(i+1)=t(i)+h;

y = newton_systeme(g,J,SP, niteration, precision);

end

newton_systeme(g,J,y0,e,Nt);

end

Answer by Adam Danz
on 11 Apr 2019

Edited by Adam Danz
on 11 Apr 2019

Just before the i-loop, you set y equal to 10.

%from your code...

y0 = 10;

y(1,:) = y0; % same as y = 10

Then on the first iteration of the i-loop, i=1 so when you execute this line

g= y(:,i+1)-y(:,i)- h*fonction;

% ----------

you're trying to access the second value of y but y only has 1 value (y=10)!

Even if that didn't cause an error this section below would (also) cause an error.

g= y(:,i+1)-y(:,i)- h*fonction;

% -----------

K>> h*fonction

Undefined operator '*' for input arguments of type 'cell'.

So there's at least a couple areas of your code you need to re-think. In other words, your errors are conceptual errors. Your code isn't written properly and that's something you'll need to work on in the context of your project.

I suggest going through your code line-by-line to confirm that it's doing what it's supposed to be doing.

Maureen Domtche
on 11 Apr 2019

Adam Danz
on 11 Apr 2019

Bob Nbob
on 12 Apr 2019

Sign in to comment.

Answer by jahanzaib ahmad
on 11 Apr 2019

there is nothing in y(:,i+1)

jahanzaib ahmad
on 11 Apr 2019

u want to minus before and after values but when u reach last value it gives error .

for for i = 1:Nt -1

the last value will not change .

Maureen Domtche
on 11 Apr 2019

i tried it didn't work , i get the same erroe message

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 9 Comments

## Bob Nbob (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692794

## Maureen Domtche (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692809

## Bob Nbob (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692813

## Maureen Domtche (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692823

## Adam Danz (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692834

## Bob Nbob (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692844

## Maureen Domtche (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692855

## Adam Danz (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692873

## Maureen Domtche (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/455787-index-in-position-2-exceed-array-bounds#comment_692874

Sign in to comment.