Attempted to access dy(0,1); index must be a positive integer or logical.
Show older comments
What am Iam I doing wrong, please? I keep getting this error message
Attempted to access dy(0,1); index must be a positive integer or logical
Error in eul (line 30)
y(i+1)=y(i)+dy(x(i),y(i))*(x(i+1)-x(i));
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)])
doing wrong, please? My code wont work. I keep getting this error message.
Answers (1)
Guillaume
on 22 Oct 2015
What is not clear about the message? Matrix indices must be strictly positive integers. You're using the elements of x to index dy, yet the first element of x is defined as 0 in the line:
x = [0,4];
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!