MATLAB: I am not able to get passed through shape function in matlab. Shape function contains symbolic variable which needs to be converted to vpa function and how to do that

galerkinpost

%this is half part of the code which is not running because of this following error:
The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in Galerkin (line 10)
N(i)=(x.^2-2*x).^i;
This is my code:
syms pi p q E A l x
n=3;
k=(n:n);
r=(1:n);
a=(1:n);
N=(1:n);
pi=eval(pi);
p=q*x/E*A*l;
for i=1:n
N(i)=(x.^2-2*x).^i; %Shape function
%N(i)=sin((2*i-1)/2*pi*x);
%N(i)=piecewise(x>=(i-1)/n & x<1/n+(i-1)/n,n*(x-(i-1)/n),x>=i/n & x<=(i+1)/n & x<1,-n*x+i+1,0);
Y1=(N(i));
Y2=diff(N(i),x);
Y3=diff(N(i),x,2);
x=linspace(0,1);
plot(x,Y1)
hold on
plot(x,Y2)
hold on
plot(x,Y3)
hold off
Legend(N,N',N'');
end

Best Answer

syms pi p q E A l x
n=3;
k=(n:n);
r=(1:n);
a=(1:n);
N = sym(1:n);
pi=eval(pi);
p=q*x/E*A*l;
for i=1:n
syms x
N(i)=(x.^2-2*x).^i; %Shape function
%N(i)=sin((2*i-1)/2*pi*x);
%N(i)=piecewise(x>=(i-1)/n & x<1/n+(i-1)/n,n*(x-(i-1)/n),x>=i/n & x<=(i+1)/n & x<1,-n*x+i+1,0);
Y1=(N(i));
Y2=diff(N(i),x);
Y3=diff(N(i),x,2);
x=linspace(0,1);
plot(x,subs(Y1))
hold on
plot(x,subs(Y2))
hold on
plot(x,subs(Y3))
hold off
legend('N','N''','N''''');
pause(1);
end