Hi MATLAB world!
I have a weird problem. I have defined 4 symbolic variables (kL, kR, kTR, kTL) for my code. I use these variables in different matrices and find their determinants with no problem.
I then have defined another variable in which I do a algebraic symbolic expression (by that I mean it includes few multiplications and additions and powers)and all of a sudden it does not recognize these variables. I have a variable lambda in there as well and it has no problem with that.
here is the error I get:
Undefined function or variable 'k'.
Error in Eoperator (line 169)
if I define k as a symbol, then it ruins my calculation but does not solve the problem either, it tells me that TL and TR and L and R are undefined.
What am I supposed to do?
Thank you all for your time and help in advance.

5 Comments

Upload your code.

*Mohammad Moeid Elahikahooker's "Answer" moved here and formatted properly:

syms lambda
syms L
syms wzero
syms wL
syms phizero
syms phiL
syms wprimezero
syms wdoubleprimezero
syms wtripleprimezero
syms kL
syms kTL
syms kR
syms kTR 
Czero=sym(zeros(4));
Czero(1,1)=cosh(lambda*L)/2;
Czero(1,2)=sinh(lambda*L)/(2*lambda);
Czero(1,3)=cosh(lambda*L)/(2*lambda*lambda);
Czero(1,4)=sinh(lambda*L)/(2*lambda*lambda*lambda);
Czero(2,1)=sinh(lambda*L)/2;
Czero(2,2)=cosh(lambda*L)/(2*lambda);
Czero(2,3)=sinh(lambda*L)/(2*lambda*lambda);
Czero(2,4)=cosh(lambda*L)/(2*lambda*lambda*lambda);
Czero(3,1)=cos(lambda*L)/2;
Czero(3,2)=sin(lambda*L)/(2*lambda);
Czero(3,3)=-1*cos(lambda*L)/(2*lambda*lambda);
Czero(3,4)=-1*sin(lambda*L)/(2*lambda*lambda*lambda);
Czero(4,1)=-1*sin(lambda*L)/2;
Czero(4,2)=cos(lambda*L)/(2*lambda);
Czero(4,3)=sin(lambda*L)/(2*lambda*lambda);
Czero(4,4)=-1*cos(lambda*L)/(2*lambda*lambda*lambda);
CL=sym(zeros(4));
CL(1,1)=1/2;
CL(1,2)=0;
CL(1,3)=1/(2*lambda*lambda);
CL(1,4)=0;
CL(2,1)=0;
CL(2,2)=1/(2*lambda);
CL(2,3)=0;
CL(2,4)=1/(2*lambda*lambda*lambda);
CL(3,1)=1/2;
CL(3,2)=0;
CL(3,3)=-1/(2*lambda*lambda);
CL(3,4)=0;
CL(4,1)=0;
CL(4,2)=1/(2*lambda);
CL(4,3)=0;
CL(4,4)=-1/(2*lambda*lambda*lambda);
% % % %  K_e check
Check2=sym(zeros(4));
Check3=sym(zeros(4));
Check4=sym(zeros(4));
Check5=sym(zeros(4));
Check6=sym(zeros(4));
Check2(:,1)=Czero(:,1);
Check2(:,2)=Czero(:,2);
Check2(:,3)=-CL(:,1);
Check2(:,4)=-CL(:,2);
Check3(:,1)=-Czero(:,4);
Check3(:,2)=-Czero(:,3);
Check3(:,3)=CL(:,4);
Check3(:,4)=CL(:,3);
Check5=inv(Check3)*Check2;
Check6=Check5;
Check6(2,:)=-Check5(2,:);
Check6(3,:)=-Check5(3,:);
Check4=Check6-transpose(Check6)
simplify(Check4,'IgnoreAnalyticConstraints',true)
% % % % % % % % % % % % % % 
BCheck2=sym(zeros(4));
BCheck3=sym(zeros(4));
BCheck4=sym(zeros(4));
BCheck5=sym(zeros(4));
BCheck6=sym(zeros(4));
Bzero=sym(zeros(4));
Bzero(1,1)=-kL*sinh(lambda*L)/(2*lambda*lambda*lambda);
Bzero(1,2)=kTL*cosh(lambda*L)/(2*lambda*lambda);
Bzero(1,3)=0;
Bzero(1,4)=0;
Bzero(2,1)=-kL*cosh(lambda*L)/(2*lambda*lambda*lambda);
Bzero(2,2)=kTL*sinh(lambda*L)/(2*lambda*lambda);
Bzero(2,3)=0;
Bzero(2,4)=0;
Bzero(3,1)=kL*sin(lambda*L)/(2*lambda*lambda*lambda);
Bzero(3,2)=-kTL*cos(lambda*L)/(2*lambda*lambda);
Bzero(3,3)=0;
Bzero(3,4)=0;
Bzero(4,1)=kL*cos(lambda*L)/(2*lambda*lambda*lambda);
Bzero(4,2)=kTL*sin(lambda*L)/(2*lambda*lambda);
Bzero(4,3)=0;
Bzero(4,4)=0;
% % % % % % 
BCheck2(:,1)=Czero(:,1)+Bzero(:,1);
BCheck2(:,2)=Czero(:,2)+Bzero(:,2);
BCheck2(:,3)=-CL(:,1);
BCheck2(:,4)=-CL(:,2);
BCheck3(:,1)=-Czero(:,4)-Bzero(:,4);
BCheck3(:,2)=-Czero(:,3)-Bzero(:,3);
BCheck3(:,3)=CL(:,4);
BCheck3(:,4)=CL(:,3);
BCheck5=inv(BCheck3)*BCheck2
BCheck6=BCheck5;
BCheck6(2,:)=-BCheck5(2,:);
BCheck6(3,:)=-BCheck5(3,:);
BCheck4=BCheck6-transpose(Check6)
simplify(BCheck4,'IgnoreAnalyticConstraints',true)
% % % % % % % % 
% % Bzero=sym(zeros(4));
% % 
% % Bzero(1,3)=-kR*sinh(lambda*L)/(2*lambda*lambda*lambda);
% % Bzero(1,4)=kTR *cosh(lambda*L)/(2*lambda*lambda);
% % 
% % 
% % Bzero(2,3)=-kR*cosh(lambda*L)/(2*lambda*lambda*lambda);
% % Bzero(2,4)=kTR *sinh(lambda*L)/(2*lambda*lambda);
% % 
% % 
% % Bzero(3,3)=kR*sin(lambda*L)/(2*lambda*lambda*lambda);
% % Bzero(3,4)=-kTR *cos(lambda*L)/(2*lambda*lambda);
% % 
% % 
% % Bzero(4,3)=kR*cos(lambda*L)/(2*lambda*lambda*lambda);
% % Bzero(4,4)=kTR *sin(lambda*L)/(2*lambda*lambda);
% % 
% % Bzero
% % 
% % B2Check2(:,1)=Czero(:,1)+Bzero(:,1);
% % B2Check2(:,2)=Czero(:,2)+Bzero(:,2);
% % B2Check2(:,3)=-CL(:,1);
% % B2Check2(:,4)=-CL(:,2);
% % B2Check3(:,1)=-Czero(:,4)-Bzero(:,4);
% % B2Check3(:,2)=-Czero(:,3)-Bzero(:,3);
% % B2Check3(:,3)=CL(:,4);
% % B2Check3(:,4)=CL(:,3);
% % B2Check5=inv(B2Check3)*B2Check2
% % B2Check6=B2Check5;
% % B2Check6(2,:)=-B2Check5(2,:);
% % B2Check6(3,:)=-B2Check5(3,:);
% % B2Check4=B2Check6-transpose(Check6)
% % simplify(B2Check4,'IgnoreAnalyticConstraints',true)
% % 
trial=[0 0 0 0; 0 0 0 0; 0 0 -kR 0; 0 0 0 kTR ];
BL=[0,0,0,kTR /(2*lambda^2); 0,0,-kR/(2*lambda^3),0; 0,0,0,-kTR /(2*lambda^2); 0,0,kR/(2*lambda^3),0];
BBCheck=BCheck6+[0 0 0 0; 0 0 0 0; 0 0 kR 0; 0 0 0 kTR ];
det_BBCheck=simplify(det(BBCheck),'IgnoreAnalyticConstraints',true)
syms paper_det
paper_det=((lambda^4)+kL*kTL)*((lambda^4)+kR*kTR )-2*(lambda^2)*((lambda^4)*kTR *kTL-kL*kR)*sinh(lambda)*sin(lambda)...
    +(2*lambda^4*(kL*kTR +kR*kTL)-(lambda^4-kL*k*TL)*(lambda^4-kR*kTR ))*cosh(lambda)*cos(lambda)...
    -lambda*((lambda^4-kL*kTL)*(lambda^2*kTR +kR)+(lambda^4-kR*kTR )*(lambda^2*kTL+kL))*sin(lambda)*cosh(lambda)...
    -lambda*((lambda^4-kL*kTL)*(lambda^2*kTR -kR)+(lambda^4-kR*kTR )*(lambda^2*kTL-kL))*cos(lambda)*sinh(lambda)
Stephen23
Stephen23 on 1 Oct 2018
Edited: Stephen23 on 1 Oct 2018

"Undefined function or variable 'k'."

Because you don't define any variable k.

"if I define k as a symbol, then it ruins my calculation but does not solve the problem either, it tells me that TL and TR and L and R are undefined."

"What am I supposed to do?"

Don't try to use variables that you have not defined.

It sounds like you expect MATLAB to somehow decompose kTL, etc, into k*TL, or something like that. This is not how symbolic manipulations work. If you want a variable k and a variable TL, then that is what you have to specify.

Hi Stephen,
Sorry for the confusion, this is actually my second iteration of the code. I tried to remove the underscore from my variables in hopes that it will help.
So I redefined k_L as kL for example and used that.
As you can see in the code, I have defined all the variables I am using and I am still getting the error.
And what's the error message now ?

Sign in to comment.

 Accepted Answer

Stephan
Stephan on 1 Oct 2018
Edited: Stephan on 1 Oct 2018
Hi,
look at the second line of paper_det - your code:
lambda^4-kL*k*TL
since there is not k and not TL defined in your code, but kTL is, i would bet, that it should be:
lambda^4-kL*kTL
which is the same structure of the calculation as the other parts of paper_det. Then the code runs without error - so it was just a typo(?)
paper_det=((lambda^4)+kL*kTL)*((lambda^4)+kR*kTR )-2*(lambda^2)*((lambda^4)*kTR *kTL-kL*kR)*sinh(lambda)*sin(lambda)...
+(2*lambda^4*(kL*kTR +kR*kTL)-(lambda^4-kL*kTL)*(lambda^4-kR*kTR ))*cosh(lambda)*cos(lambda)...
-lambda*((lambda^4-kL*kTL)*(lambda^2*kTR +kR)+(lambda^4-kR*kTR )*(lambda^2*kTL+kL))*sin(lambda)*cosh(lambda)...
-lambda*((lambda^4-kL*kTL)*(lambda^2*kTR -kR)+(lambda^4-kR*kTR )*(lambda^2*kTL-kL))*cos(lambda)*sinh(lambda)
Best regards
Stephan

1 Comment

Thanks Stephan,
days of working on multiple codes in a rush for a paper had blinded me to that mistake.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!