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;endfor 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);endy,yyplot(x,y,'g-')hold onplot(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)]endend
MATLAB: 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
errorhybridscheme
Best Answer