필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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
Stephen23
Stephen23 2016년 6월 13일
편집: Stephen23 2016년 6월 13일
Indices can only be integer values greater than zero. What value do you think y(i+1/2) is going to be ? Does this fit the accepted definition of an index ?

댓글을 달려면 로그인하십시오.

채택된 답변

David H
David H 2016년 6월 13일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by