Post Reply 
Demonstration program for using CAS with PPL
01-18-2014, 01:46 AM (This post was last modified: 01-21-2014 09:05 PM by Han.)
Post: #2
RE: Demonstration program for using CAS with PPL
Here's a modification of your code to use only local variables. Moreover, after the program is finished, you won't be left with a lot of temporary CAS variables of the form \( a_{ij\underline{ }} \) active in the CAS view. Lastly, no global variables are altered in the process.

The idea is that since the command will ultimately be delimited by double quotes, then we can use local variables and string addition to do any intermediate calculations which do not actually involve CAS commands. The string manipulation can then be saved as a local variable. So using CAS(locavariable) is fine because the CAS() command itself is a non-CAS command, so it can handle local variables. If localvariable is a string, then all is well.

PHP Code:
EXPORT Beam2_Solve(matrix,guess)
BEGIN
  LOCAL Save_AAngle
:=AAngle;
  
LOCAL soln;

  
// nSolve(DET(matrix)=0,x=guess)
  
matrix:="nSolve(DET(" matrix ")=0,x=" guess ")";

  
AAngle:=1// set radian mode

  // ensure that user's angle mode is always restored, even if error occurs
  
IFERR soln:=CAS(matrix); THEN  END;
  
AAngle:=Save_AAngle;
  RETURN 
ROUND(ABS(soln),6);
END;

//Program to solve for natural frequency of a beam with flexible end supports
//
// k1 = left support translational spring constant
// k2 = left support rotational spring constant
// k3 = right support translational spring constant
// k4 = right support rotational spring constant
// EI = product of beam material modulus of elasticity and bending moment of inertia
// m = beam mass per unit length
// L = beam length
// guess = guess for eigenvalue solver
//
EXPORT Beam2_Main(k1,k2,k3,k4,EI,m,L,guess)
BEGIN

  LOCAL a
,b,c,d,s1,s2,s;
  
LOCAL matname="m0";

  
// initialize matrix rows to form that does not involve pre-computed values
  
LOCAL row1="[[0,-1,0,-1],";
  
LOCAL row2="[-1,0,-1,0],";
  
LOCAL row3="[-SIN(x),-COS(x),-SINH(x),-COSH(x)],";
  
LOCAL row4="[COS(x),-SIN(x),COSH(x),SINH(x)]]";

  
a:=k1*L^3/EI;  b:=k2*L/EI;
  
c:=k3*L^3/EI;  d:=k4*L/EI;

  
// rebuild rows only as necessary
  
IF a>=0 THEN
    row1
:="[[x^3,-" ",-x^3,-" "],";
  
END;
  
  IF 
b>=0 THEN
    row2
:="[-" ",-x,-" ",x],";
  
END;

  IF 
c>=0 THEN
    row3
:="[-x^3*COS(x)-" +  "*SIN(x),x^3*SIN(x)-" "*COS(x),x^3*COSH(x)-" "*SINH(x),x^3*SINH(x)-" "*COSH(x)],";
  
END;

  IF 
d>=0 THEN
    row4
:="[-x*SIN(x)+" "*COS(x),-x*COS(x)-" "*SIN(x),x*SINH(x)+" "*COSH(x),x*COSH(x)+" "*SINH(x)]]";
  
END;

  
// ask user to save matrix
  
s:=INPUT(matname,"Save Matrix?","Name=","Enter CAS name for matrix",matname);
  if 
AND TYPE(matname)==2 then
    matname
:=matname+":="+row1+row2+row3+row4;
    
iferr CAS(matname); then msgbox("Unable to save matrix."); end;
  
end;

  
// send subroutine both the string to pass to CAS() and guess
  
s1:=Beam2_Solve(row1+row2+row3+row4,guess);
  
s2:=(s1/L)^2*(EI/m)^.5/(2*pi);
  RETURN {
ROUND(s1,6),ROUND(s2,3)};
END

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Demonstration program for using CAS with PPL - Han - 01-18-2014 01:46 AM



User(s) browsing this thread: 1 Guest(s)