Azzera filtri
Azzera filtri

Unable to convert expression containing symbolic variables into double array.

12 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 7 Feb 2021
%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
wij = zeros(m,N,'sym');
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)*wij(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 %s\n',x, t, char(vpa(wij(i,j),10)));
end
end
0.1 0.1 0.0 0.2 0.1 0.5877852523 0.3 0.1 0.8090169944 0.4 0.1 0.9510565163 0.5 0.1 1.0 0.6 0.1 0.9510565163 0.7 0.1 0.8090169944 0.8 0.1 0.5877852523 0.9 0.1 0.3090169944 1.0 0.1 0.0 0.1 0.1 0.0 0.2 0.1 0.02938926261*cos(6.283185307*t) + 0.7135254916 0.3 0.1 0.04045084972*cos(6.283185307*t) + 1.06331351 0.4 0.1 0.04755282581*cos(6.283185307*t) + 1.309016994 0.5 0.1 0.05*cos(6.283185307*t) + 1.426584774 0.6 0.1 0.04755282581*cos(6.283185307*t) + 1.404508497 0.7 0.1 0.04045084972*cos(6.283185307*t) + 1.244949142 0.8 0.1 0.02938926261*cos(6.283185307*t) + 0.9635254916 0.9 0.1 0.01545084972*cos(6.283185307*t) + 0.5877852523 1.0 0.1 0.0 0.1 0.2 0.0 0.2 0.2 0.04045084972*cos(6.283185307*t) + 0.4755282581 0.3 0.2 0.07694208843*cos(6.283185307*t) + 1.213525492 0.4 0.2 0.09045084972*cos(6.283185307*t) + 1.538841769 0.5 0.2 0.09510565163*cos(6.283185307*t) + 1.713525492 0.6 0.2 0.09045084972*cos(6.283185307*t) + 1.720477401 0.7 0.2 0.07694208843*cos(6.283185307*t) + 1.559016994 0.8 0.2 0.05590169944*cos(6.283185307*t) + 1.244949142 0.9 0.2 0.02938926261*cos(6.283185307*t) + 0.6545084972 1.0 0.2 0.0 0.1 0.2 0.0 0.2 0.2 0.04755282581*cos(6.283185307*t) + 0.5 0.3 0.2 0.09045084972*cos(6.283185307*t) + 0.9510565163 0.4 0.2 0.1244949142*cos(6.283185307*t) + 1.618033989 0.5 0.2 0.1309016994*cos(6.283185307*t) + 1.832734395 0.6 0.2 0.1244949142*cos(6.283185307*t) + 1.868033989 0.7 0.2 0.1059016994*cos(6.283185307*t) + 1.720477401 0.8 0.2 0.07694208843*cos(6.283185307*t) + 1.25 0.9 0.2 0.04045084972*cos(6.283185307*t) + 0.6571638901 1.0 0.2 0.0 0.1 0.2 0.0 0.2 0.2 0.05*cos(6.283185307*t) + 0.4755282581 0.3 0.2 0.09510565163*cos(6.283185307*t) + 0.9045084972 0.4 0.2 0.1309016994*cos(6.283185307*t) + 1.244949142 0.5 0.2 0.1538841769*cos(6.283185307*t) + 1.772542486 0.6 0.2 0.1463525492*cos(6.283185307*t) + 1.832734395 0.7 0.2 0.1244949142*cos(6.283185307*t) + 1.559016994 0.8 0.2 0.09045084972*cos(6.283185307*t) + 1.132692148 0.9 0.2 0.04755282581*cos(6.283185307*t) + 0.5954915028 1.0 0.2 0.0 0.1 0.3 0.0 0.2 0.3 0.04755282581*cos(6.283185307*t) + 0.4045084972 0.3 0.3 0.09045084972*cos(6.283185307*t) + 0.7694208843 0.4 0.3 0.1244949142*cos(6.283185307*t) + 1.059016994 0.5 0.3 0.1463525492*cos(6.283185307*t) + 1.244949142 0.6 0.3 0.1538841769*cos(6.283185307*t) + 1.463525492 0.7 0.3 0.1309016994*cos(6.283185307*t) + 1.244949142 0.8 0.3 0.09510565163*cos(6.283185307*t) + 0.9045084972 0.9 0.3 0.05*cos(6.283185307*t) + 0.4755282581 1.0 0.3 0.0 0.1 0.4 0.0 0.2 0.4 0.04045084972*cos(6.283185307*t) + 0.2938926261 0.3 0.4 0.07694208843*cos(6.283185307*t) + 0.5590169944 0.4 0.4 0.1059016994*cos(6.283185307*t) + 0.7694208843 0.5 0.4 0.1244949142*cos(6.283185307*t) + 0.75 0.6 0.4 0.1309016994*cos(6.283185307*t) + 0.6571638901 0.7 0.4 0.1244949142*cos(6.283185307*t) + 0.8090169944 0.8 0.4 0.09045084972*cos(6.283185307*t) + 0.5877852523 0.9 0.4 0.04755282581*cos(6.283185307*t) + 0.3090169944 1.0 0.4 0.0 0.1 0.4 0.0 0.2 0.4 0.02938926261*cos(6.283185307*t) + 0.1545084972 0.3 0.4 0.05590169944*cos(6.283185307*t) + 0.2938926261 0.4 0.4 0.07694208843*cos(6.283185307*t) + 0.25 0.5 0.4 0.09045084972*cos(6.283185307*t) + 0.181635632 0.6 0.4 0.09510565163*cos(6.283185307*t) + 0.09549150281 0.7 0.4 0.09045084972*cos(6.283185307*t) 0.8 0.4 0.07694208843*cos(6.283185307*t) + 0.2135254916 0.9 0.4 0.04045084972*cos(6.283185307*t) + 0.1122569941 1.0 0.4 0.0 0.1 0.5 0.0 0.2 0.5 0.01545084972*cos(6.283185307*t) 0.3 0.5 0.02938926261*cos(6.283185307*t) - 0.1545084972 0.4 0.5 0.04045084972*cos(6.283185307*t) - 0.2938926261 0.5 0.5 0.04755282581*cos(6.283185307*t) - 0.4045084972 0.6 0.5 0.05*cos(6.283185307*t) - 0.4755282581 0.7 0.5 0.04755282581*cos(6.283185307*t) - 0.5 0.8 0.5 0.04045084972*cos(6.283185307*t) - 0.4755282581 0.9 0.5 0.02938926261*cos(6.283185307*t) - 0.09549150281 1.0 0.5 0.0 0.1 0.5 0.0 0.2 0.5 -0.3090169944 0.3 0.5 -0.5877852523 0.4 0.5 -0.8090169944 0.5 0.5 -0.9510565163 0.6 0.5 -1.0 0.7 0.5 -0.9510565163 0.8 0.5 -0.8090169944 0.9 0.5 -0.5877852523 1.0 0.5 0.0 0.1 0.6 0.0 0.2 0.6 - 0.01545084972*cos(6.283185307*t) - 0.5877852523 0.3 0.6 - 0.02938926261*cos(6.283185307*t) - 0.9635254916 0.4 0.6 - 0.04045084972*cos(6.283185307*t) - 1.244949142 0.5 0.6 - 0.04755282581*cos(6.283185307*t) - 1.404508497 0.6 0.6 - 0.05*cos(6.283185307*t) - 1.426584774 0.7 0.6 - 0.04755282581*cos(6.283185307*t) - 1.309016994 0.8 0.6 - 0.04045084972*cos(6.283185307*t) - 1.06331351 0.9 0.6 - 0.02938926261*cos(6.283185307*t) - 0.7135254916 1.0 0.6 0.0 0.1 0.6 0.0 0.2 0.6 - 0.02938926261*cos(6.283185307*t) - 0.6545084972 0.3 0.6 - 0.05590169944*cos(6.283185307*t) - 1.244949142 0.4 0.6 - 0.07694208843*cos(6.283185307*t) - 1.559016994 0.5 0.6 - 0.09045084972*cos(6.283185307*t) - 1.720477401 0.6 0.6 - 0.09510565163*cos(6.283185307*t) - 1.713525492 0.7 0.6 - 0.09045084972*cos(6.283185307*t) - 1.538841769 0.8 0.6 - 0.07694208843*cos(6.283185307*t) - 1.213525492 0.9 0.6 - 0.04045084972*cos(6.283185307*t) - 0.4755282581 1.0 0.6 0.0 0.1 0.7 0.0 0.2 0.7 - 0.04045084972*cos(6.283185307*t) - 0.6571638901 0.3 0.7 - 0.07694208843*cos(6.283185307*t) - 1.25 0.4 0.7 - 0.1059016994*cos(6.283185307*t) - 1.720477401 0.5 0.7 - 0.1244949142*cos(6.283185307*t) - 1.868033989 0.6 0.7 - 0.1309016994*cos(6.283185307*t) - 1.832734395 0.7 0.7 - 0.1244949142*cos(6.283185307*t) - 1.618033989 0.8 0.7 - 0.09045084972*cos(6.283185307*t) - 0.9510565163 0.9 0.7 - 0.04755282581*cos(6.283185307*t) - 0.5 1.0 0.7 0.0 0.1 0.7 0.0 0.2 0.7 - 0.04755282581*cos(6.283185307*t) - 0.5954915028 0.3 0.7 - 0.09045084972*cos(6.283185307*t) - 1.132692148 0.4 0.7 - 0.1244949142*cos(6.283185307*t) - 1.559016994 0.5 0.7 - 0.1463525492*cos(6.283185307*t) - 1.832734395 0.6 0.7 - 0.1538841769*cos(6.283185307*t) - 1.772542486 0.7 0.7 - 0.1309016994*cos(6.283185307*t) - 1.244949142 0.8 0.7 - 0.09510565163*cos(6.283185307*t) - 0.9045084972 0.9 0.7 - 0.05*cos(6.283185307*t) - 0.4755282581 1.0 0.7 0.0 0.1 0.8 0.0 0.2 0.8 - 0.05*cos(6.283185307*t) - 0.4755282581 0.3 0.8 - 0.09510565163*cos(6.283185307*t) - 0.9045084972 0.4 0.8 - 0.1309016994*cos(6.283185307*t) - 1.244949142 0.5 0.8 - 0.1538841769*cos(6.283185307*t) - 1.463525492 0.6 0.8 - 0.1463525492*cos(6.283185307*t) - 1.244949142 0.7 0.8 - 0.1244949142*cos(6.283185307*t) - 1.059016994 0.8 0.8 - 0.09045084972*cos(6.283185307*t) - 0.7694208843 0.9 0.8 - 0.04755282581*cos(6.283185307*t) - 0.4045084972 1.0 0.8 0.0 0.1 0.8 0.0 0.2 0.8 - 0.04755282581*cos(6.283185307*t) - 0.3090169944 0.3 0.8 - 0.09045084972*cos(6.283185307*t) - 0.5877852523 0.4 0.8 - 0.1244949142*cos(6.283185307*t) - 0.8090169944 0.5 0.8 - 0.1309016994*cos(6.283185307*t) - 0.6571638901 0.6 0.8 - 0.1244949142*cos(6.283185307*t) - 0.75 0.7 0.8 - 0.1059016994*cos(6.283185307*t) - 0.7694208843 0.8 0.8 - 0.07694208843*cos(6.283185307*t) - 0.5590169944 0.9 0.8 - 0.04045084972*cos(6.283185307*t) - 0.2938926261 1.0 0.8 0.0 0.1 0.9 0.0 0.2 0.9 - 0.04045084972*cos(6.283185307*t) - 0.1122569941 0.3 0.9 - 0.07694208843*cos(6.283185307*t) - 0.2135254916 0.4 0.9 -0.09045084972*cos(6.283185307*t) 0.5 0.9 - 0.09510565163*cos(6.283185307*t) - 0.09549150281 0.6 0.9 - 0.09045084972*cos(6.283185307*t) - 0.181635632 0.7 0.9 - 0.07694208843*cos(6.283185307*t) - 0.25 0.8 0.9 - 0.05590169944*cos(6.283185307*t) - 0.2938926261 0.9 0.9 - 0.02938926261*cos(6.283185307*t) - 0.1545084972 1.0 0.9 0.0 0.1 0.9 0.0 0.2 0.9 0.09549150281 - 0.02938926261*cos(6.283185307*t) 0.3 0.9 0.4755282581 - 0.04045084972*cos(6.283185307*t) 0.4 0.9 0.5 - 0.04755282581*cos(6.283185307*t) 0.5 0.9 0.4755282581 - 0.05*cos(6.283185307*t) 0.6 0.9 0.4045084972 - 0.04755282581*cos(6.283185307*t) 0.7 0.9 0.2938926261 - 0.04045084972*cos(6.283185307*t) 0.8 0.9 0.1545084972 - 0.02938926261*cos(6.283185307*t) 0.9 0.9 -0.01545084972*cos(6.283185307*t) 1.0 0.9 0.0 0.1 1.0 0.0 0.2 1.0 0.5877852523 0.3 1.0 0.8090169944 0.4 1.0 0.9510565163 0.5 1.0 1.0 0.6 1.0 0.9510565163 0.7 1.0 0.8090169944 0.8 1.0 0.5877852523 0.9 1.0 0.3090169944 1.0 1.0 0.0 0.1 1.0 0.0 0.2 1.0 0.02938926261*cos(6.283185307*t) + 0.7135254916 0.3 1.0 0.04045084972*cos(6.283185307*t) + 1.06331351 0.4 1.0 0.04755282581*cos(6.283185307*t) + 1.309016994 0.5 1.0 0.05*cos(6.283185307*t) + 1.426584774 0.6 1.0 0.04755282581*cos(6.283185307*t) + 1.404508497 0.7 1.0 0.04045084972*cos(6.283185307*t) + 1.244949142 0.8 1.0 0.02938926261*cos(6.283185307*t) + 0.9635254916 0.9 1.0 0.01545084972*cos(6.283185307*t) + 0.5877852523 1.0 1.0 0.0
  2 Commenti
LightFury Yeji
LightFury Yeji il 7 Feb 2021
Thank you so much for this! Is there any way I can only display the first eleven lines of the answer?
0.1 0.1 0.0
0.2 0.1 0.5877852523
0.3 0.1 0.8090169944
0.4 0.1 0.9510565163
0.5 0.1 1.0
0.6 0.1 0.9510565163
0.7 0.1 0.8090169944
0.8 0.1 0.5877852523
0.9 0.1 0.3090169944
1.0 0.1 0.0
Walter Roberson
Walter Roberson il 7 Feb 2021
%STEP6
stopping_loop = false;
for j=1:N
t=j*k;
for i=1:m
x=i*h;
if ~isempty(symvar(wij(i,j))
stopping_loop = true;
break;
end
fprintf( '%.1f %.1f %.10f\n',x, t, double(wij(i,j)));
end
if stopping_loop; break; end
end

Accedi per commentare.

Più risposte (1)

toka hakim
toka hakim il 21 Mag 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

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by