I am following this algorithm (pic) and in Step 4, in the line "wij(i,2)=(1-lambda^2)*f(i*h)+(lambda^2/2)*(f((i+1)*h)+f((i-1)*h))+k*g(i*h);" I am getting the error "Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables." I tried applying subs to f(i*h) and g(i*h), so it was wij(i,2)=(1-lambda^2)*(subs(f(x),x,(i*h)))+(lambda^2/2)*(subs(f(x),x,((i+1)*h)))+(subs(f(x),x,((i-1)*h)))+k*(subs(g(x),x,(i*h))); but I still get the same error. Please help
This is my whole program:
%Problem:
l=1; %endpoint
T=1; %maximum time
alpha = 2; %constant
m=10; N=20;syms f(x) g(x,t)f(x)=sin(pi*x);g(x)=(sin(pi*x))*(cos(2*pi*t));%STEP1
h=l/m;k=T/N;lambda=k*alpha/h;%STEP2
for j=2:N wij(1,j)=0; %wij(0,j)
wij(m,j)=0;end%STEP3
wij(1,1)=f(0); %wij(0,0)
wij(m,1)=f(l); %STEP4
for i=2:m-1 wij(i,1)=subs(f(x),x,(i*h)); %wij(i,0)
wij(i,2)=(1-lambda^2)*(subs(f(x),x,(i*h)))+(lambda^2/2)*(subs(f(x),x,((i+1)*h)))+(subs(f(x),x,((i-1)*h)))+k*(subs(g(x),x,(i*h)));end%STEP5
for j=2:N-1 for i=2:m-1 wij(i,j+1)=2*(1-lambda^2)*w(i,j)+lambda^2*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1); endend%STEP6
for j=1:N t=j*k; for i=1:m x=i*h; fprintf( '%.1f %.1f %.10f',x,t,wij(i,j)) endend
Best Answer