Index in position 2 exceed array bounds

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

9 Comments

Which line gives the error?
this one " g= y(:,i+1)-y(:,i)- h*fonction; "
Are you intentionally recalculating y inside of your loop? Is it supposed to be the same y as y(1,:) = y0? Does the new calculation of y have the same size as the original?
No the size is not supposed to remain de same.Its supposed to grow along with the iterations.i wrote y(1,:)=y0 , just as an a starting point like itteration 0.What can do plz to not have that error anymore?While making sure y0 stay de first element.
Could you provide us with the values of the inputs?
fonction, J ,t0,y0, h,e, Nt
So y0 is a single value?
If that is true then the first iteration of y will also be a single values, so the size of y is 1x1. It is impossible then to gather y(1,2) (this comes from y(1,i+1) where i = 1 during the first iteration), which is causing the error.
To work around this you either need to add some kind of conditional statement for calculating g when i = 1, or you need to change the loop or equation to not look for values that don't exist yet.
If y0 is not a single value, then yes, please provide sample inputs for the function, as Adam requested.
N = 100;
b = 1;
u = 0.05;r = 0.5;
nmax = 100;
precision = 1e-2;
h=0.5;
t0=0;
y0=10
sp= @(S,I) u*N-u*S-b/N*S*I;
ip = @(S,I) (b/N)*S*I-r*I-u*I;
fonction = {sp, ip};
J = {@(S,I)-u-b/N*I, @(S,I)-b/N*S;
@(S,I)(b/N)*I, @(S,I)(b/N)*S-r-u};
What about Nt (and e)?
Nt = 200 e = 0.05

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 11 Apr 2019
Edited: 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.

3 Comments

I tried going line by line to figure out whats wrong with my code and it turns out that the problematic line is this one "g= y(:,n+1)-y(:,n)- h*f(y(:,n),t(n+1)); " So if any of you guys have ideas on how to fix that let me know please
We don't have enough information to fix it (or confirm that it's broken). It's just a bunch of letters to us.
What you need to do to fix your code is to figure out how to get more than one element in y. As has been mentioned already the calculation of g is looking for two elements of y when only one exists. We are not particularly able to help you fix this particular problem because it is a very contextual solution, not something to do with the code. All we can tell you is that this is why the code isn't working.

Sign in to comment.

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

2 Comments

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 .
i tried it didn't work , i get the same erroe message

Sign in to comment.

Categories

Asked:

on 11 Apr 2019

Commented:

on 12 Apr 2019

Community Treasure Hunt

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

Start Hunting!