Unable to convert expression containing symbolic variables into double array.
Show older comments
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);
end
end
%STEP6
for j=1:N
t=j*k;
for i=1:m
x=i*h;
fprintf( '%.1f %.1f %.10f',x,t,wij(i,j))
end
end
Accepted Answer
More Answers (1)
toka hakim
on 21 May 2021
syms x;
f=cos(x)-x;
x1=0.5;
x2=pi/4;
for i=3:6
x(i)=x(i-1)-(f(x(i-1))*(x(i-1)-x(i-2)))/f(x(i-1)-f(x(i-2)));
vpa(x(i),10);
end
1 Comment
Walter Roberson
on 21 May 2021
It is not clear how this is an Answer to the question that was asked?
Categories
Find more on Conversion Between Symbolic and Numeric 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!