i keep getting this Attempted to access y(1.5); index must be a positive integer or logical. when trying to implement a numerical hybrid scheme involving y(i+1/2) . here's the code so far: the problem is in line36
    8 views (last 30 days)
  
       Show older comments
    
    Matthew Atugege
 on 13 Jun 2016
  
    
    
    
    
    Commented: Matthew Atugege
 on 13 Jun 2016
            function[x,y]=secondode1(x0,x1,y0,z0,n)
format long 
f=@(x,y,z)z;
g=@(x,y,z) -6/x*z-4/x^(2)*y;
x0=1;
x1=0.003125;
y0=1;
z0=1;
n=10;
h=0.003125;
%preallocation space for x and y
%yy and xx means yexact and xexact respectively
x=[x0,zeros(1,15)];
y=[y0,zeros(1,15)];
z=[z0,zeros(1,15)];
xx=zeros(1,15);
yy=zeros(1,15);
for i=1:3
    x(i+1)=x(i)+h;
    k1=h*f(x(i),y(i),z(i));
    l1=h*g(x(i),y(i),z(i));
    k2=h*f(x(i)+(h/2),y(i)+(k1/2),z(i)+(l1/2));
    l2=h*g(x(i)+(h/2),y(i)+(k1/2),z(i)+(l1/2));
    k3=h*f(x(i)+(h/2),y(i)+(k2/2),z(i)+(l2/2));
    l3=h*g(x(i)+(h/2),y(i)+(k2/2),z(i)+(l2/2));
    k4=h*f(x(i)+h,y(i)+k3,z(i)+l3);
    l4=h*g(x(i)+h,y(i)+k3,z(i)+l3);
    k=(k1+(2*k2)+(2*k3)+k4)/6;
    l=(l1+(2*l2)+(2*l3)+l4)/6;
    y(i+1)=y(i)+k;
    z(i+1)=z(i)+l;
end
for i=1:10
   x(i+1)=x(i)+h;
   y(i+2)=-3*y(i)+4*y(i+1/2)+(h^(2))/32*(f(x(i+2),y(i+2),z(i+2))+38*f(x(i+1),y(i+1),z(i+1))+9*f(x(i),y(i),z(i)));
    xx(i+1)=xx(i)+h;
    yy(i+1)=xx(i)+h;
    yy(i+1)=(5*x.^3-2)/(3*x.^4);
end
y,yy
plot(x,y,'g-')
hold on
plot(xx,yy,'r:')
xlabel('x axis'),ylabel('y axis'),title('the graph f the scheme solution and exact solution'),legend('scheme','exact')
for i=1:10
  [[y(i+2)]' [yy(i+1)]']
  error=[y(i+2)-yy(i+1)]
end
end
1 Comment
Accepted Answer
  David H
      
 on 13 Jun 2016
        You can't call an index of an array with non-positive integer arguments. There is no 3/2 th entry of a matrix or vector, that doesn't make any sense.
Therefore y(i+1/2) will obvious return an error if i is an integer, do you perhaps mean to write y((i+1)/2) or (y(i)+y(i+1))/2?
David
More Answers (0)
See Also
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!

