Search code examples
matlabanonymous-functionsymbolic-math

How to assign a vector to a symbolic expression or function handle?


clear all
clc
k=1;
z3=[0.21,0.132,0.094,0.073,0.0601,0.0509];
b3=[4.7293,7.8532,10.9912,14.1372,17.2788,20.4204];
syms x
a=1/(4*b3(k)^3*(1-cosh(b3(k))*cos(b3(k))));
t1=sinh(b3(k)*x)+sin(b3(k)*x);
t2=cosh(b3(k)*x)+cos(b3(k)*x);
% first part of green function 
s1=subs(t1,x,(1-z3(k)));
s2=subs(t2,x,(1-z3(k)));
m1=(cosh(b3(k))-cos(b3(k)))*((t1*s2)+(t2*s1));
m2=sinh(b3(k))*((t1*s1)+(t2*s2));
m3=sin(b3(k))*((t1*s1)-(t2*s2));
w1=a*(m1-m2-m3);
w1= matlabFunction(w1)
% second part of green function 
s3=subs(t1,x,z3(k));
s4=subs(t1,x,(1-x));
s5=subs(t2,x,z3(k));
s6=subs(t2,x,(1-x));
n1=(cosh(b3(k))-cos(b3(k)))*((s3*s6)+(s5*s4));
n2=sinh(b3(k))*((s3*s4)+(s5*s6));
n3=sin(b3(k))*((s3*s4)-(s5*s6));
w2=a*(n1-n2-n3);
w2= matlabFunction(w2)
x=0:0.01:1;
c=0:0.01:1;
for i=1:length(x)
    if (x(i)<=z3(k))
        w(i)=w1;
        % below is the second half of the green function
    elseif (x(i)>=z3(k))
        w(i)=w2;
    end
end
plot(x,w)

I got a symbolic expression, I want to assign the value of x which is a vector, and extract "w" which is a piecewise. I am finding some difficulty in solving this."Nonscalar arrays of function handles are not allowed" is the error I am getting.


Solution

  • First w1 and w2 are not symbolic expressions, but normal anonymous function handles. Hence I guess you just want to insert x(i) into w1 or w2, do you?

    Second, in the line x=0:0.01:1; you are creating an array of doubles x and overwrite the previously defined symbolic variable syms x, which later becomes the function argument of your symbolic handle. In this case nothing happened, but I recommend to rename the vector.

    X=0:0.01:1;
    for ii=1:length(xX)
        if (X(ii)<=z3(k))
            w(ii) = w1(X(ii));
            % below is the second half of the green function
        elseif (x(ii)>=z3(k))
            w(ii)=w2(X(ii));
        end
    
    end
    

    enter image description here

    Before you were just assigning the handles to w, which would require a cell array. But I doubt you wanted that.