Question: how do I handle rho:=x[n]/h in this code?

Good day, all. I hope we are staying safe.

Please I need help with the regard to the following codes.

The major issue is with rho:=x[n]/h. The rho needs to change values as x[n] changes in the computation. This I think I have gotten wrong. Your professional modifications or/and corrections to the code would be appreciated.

Thank you and kind regards.

restart;
Digits:=20:
#assume(alpha>0,alpha < 1):
f:=proc(n)
	-y[n]-x[n]+(x[n])^2+(2*(x[n])^(2-alpha))/GAMMA(3-alpha)+((x[n])^(1-alpha))/GAMMA(2-alpha):
end proc:

e2:=y[n+2] = y[n]+(1/2)*((alpha^2-alpha)*rho^2+(2*alpha^2-2)*rho+(4/3)*alpha^2-(2/3)*alpha)*(rho*h)^alpha*GAMMA(2-alpha)*f(n)/rho-alpha*GAMMA(2-alpha)*((-1+alpha)*rho^2+(2*alpha-2)*rho+(4/3)*alpha-8/3)*(h*(rho+1))^alpha*f(n+1)/(rho+1)+(1/2)*((alpha^2-alpha)*rho^2+2*(-1+alpha)^2*rho+(4/3)*alpha^2-(14/3)*alpha+4)*(h*(rho+2))^alpha*f(n+2)*GAMMA(2-alpha)/(rho+2):
e1:=y[n+1] = y[n]+(1/4)*((alpha^2-alpha)*rho^2+(alpha^2+3*alpha-4)*rho+(1/3)*alpha^2+(4/3)*alpha)*GAMMA(2-alpha)*f(n)/((rho*h)^(-alpha)*rho)-(1/2)*GAMMA(2-alpha)*((alpha^2-alpha)*rho^2+(alpha^2+alpha-2)*rho+(1/3)*alpha^2+(1/3)*alpha-2)*f(n+1)/((h*(rho+1))^(-alpha)*(rho+1))+(1/4)*((-1+alpha)*rho^2+(-1+alpha)*rho+(1/3)*alpha-2/3)*alpha*GAMMA(2-alpha)*f(n+2)/((h*(rho+2))^(-alpha)*(rho+2)):


alpha:=0.25:
inx:=0:
iny:=0:
#x[0]:=0:
h:=1/20:
N:=solve(h*p = 1, p):
n:=0:
c:=1:
rho:=x[n]/h: 
err := Vector(round(N)):
exy_lst := Vector(round(N)):

for j from 0 to 2 do
	t[j]:=inx+j*h:
end do:
vars:=y[n+1],y[n+2]:

step := [seq](eval(x, x=c*h), c=1..N):

printf("%4s%15s%15s%16s\n", 
	"h","Num.y","Ex.y","Error y");
st := time():
for k from 1 to N/2 do

	par1:=x[n]=t[0],x[n+1]=t[1],x[n+2]=t[2]:
	par2:=y[n]=iny:
	res:=eval(<vars>, fsolve(eval({e||(1..2)},[par1,par2]), {vars}));

	for i from 1 to 2 do
		exy:=eval(-c*h+(c*h)^2):
		printf("%5.3f%17.9f%15.9f%15.5g\n", 
		h*c,res[i],exy,abs(res[i]-exy)):
		
		err[c] := abs(evalf(res[i]-exy)):
		exy_lst[c] := exy:
		numerical_y1[c] := res[i]:
		c:=c+1:
		rho:=rho+1:
	end do:
	iny:=res[2]:
	inx:=t[2]:
	for j from 0 to 2 do
		t[j]:=inx + j*h:
	end do:
end do:
v:=time() - st;
printf("Maximum error is %.13g\n", max(err));

numerical_array_y1 := [seq(numerical_y1[i], i = 1 .. N)]:

time_t := [seq](step[i], i = 1 .. N):

with(plots):
numerical_plot_y1 := plot(time_t, numerical_array_y1, style = [point], symbol = [asterisk],
				color = [blue,blue],symbolsize = 15, title="y numerical",legend = ["Numerical"]);
exact_plot_y1 := plot(time_t, exy_lst, style = [line], symbol = [box], 
				color = [red,red], symbolsize = 10, title="y exact",legend = ["Exact"]);

display({numerical_plot_y1, exact_plot_y1}, title = "Exact and Numerical Solution of Example 1 ");

 

Please Wait...