index must be a positive integer or logical
Mostrar comentarios más antiguos
I am trying to solve differential using Euler's approach. When running my code I keep getting this error message "Attempted to access dy(0,1); index must be a positive integer or logical". What am I doing wrong, please?
for i=1:n-1
iter=0;
y(i+1)=y(i)+dy(x(i),y(i))*(x(i+1)-x(i));
iter=iter+1;
end
disp([x(i),y(i+1)])
[Full code merged from duplicate question]
clc
clear all
close all
x=[0,4];
y=-0.5*x.^4+4*x.^3-10*x.^2+8.5*x+1;
dy=-2*x.^3+12*x.^2-20*x+8.5;
y_0=1;
h=0.5;
if nargin<4,
error('at least 4 input arguments required')
end
xi=x(1);
xf=x(2);
if ~(xf>xi),
error('upper limit must be greater than lower')
end
x=(xi:h:xf)';
n=length(x);
if x(n)<xf
x(n+1)=xf;
n=n+1;
end
y=y_0*ones(n,1);
for i=1:n-1
iter=0;
y(i+1)=y(i)+dy(x(i),y(i))*(x(i+1)-x(i));
iter=iter+1;
end
disp([x(i),y(i+1)])
Respuestas (1)
James Tursa
el 22 de Oct. de 2015
0 votos
What are the values of the x vector? E.g., is x(1) equal to 0? This could cause the error.
1 comentario
Adeyinka Ademola
el 22 de Oct. de 2015
Editada: James Tursa
el 22 de Oct. de 2015
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!