正在加载图片...
ATLAB Lecture 5 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr >>f2=sym(a*x 2+b*x+c) >>12=a*x^2+b*x+c In general, you can use sym or syms to create symbolic variables. We recommend you use syms because it requires less typing If you want to generation an symbolic equation, the code f2=a*x 2+b x+c=0 is illegal The correct command is f2=sym(a*x 2+bx+c=0)or f2=sym(a*x 2+b*x+c=0") Note To create a symbolic expression that is a constant, you must use the sym command >>s 5=sym(5); s 5/3 create a symbolic 5 5/3 create a number 5 1.6667 If you set a variable equal to a symbolic expression, and then apply the syms command to the variable, MATLAB removes the previously defined expression from the variable >> syms a >>f3=a+b %create a symbolic variable f, whose value is a symbolic expression a+b 13 > syms f3 removes the previously defined expression from the variable >>f ☆ The subs command You can substitute a numerical value for a symbolic variable using the subs command to >>f4=2*x2-3x+I': %create a'charvariable f4, equivalent to sym f4 f4=2*x^2-3*x+1 >> subs(f4, 1) %substitute the val 0 *Note To substitute a matrix A into the symbolic expression f, use the command polyvalm( sym2poly(f), A), which replaces all occurrences of x by A, and replaces the constant term of f with the constant times an identity matrix A=[1234 3 polyram( Lec5-3MATLAB Lecture 5  School of Mathematical Sciences Xiamen University  http://gdjpkc.xmu.edu.cn  Lec5­3  >> syms a b c x  >> f2= sym('a*x^2 + b*x + c') or >> f2= a*x^2 + b*x + c In general, you can use sym or syms to create symbolic variables. We recommend you use syms because it requires less typing.  If you want to generation an symbolic equation, the code f2= a*x^2 + b*x + c=0 is illegal.  The correct command is f2= sym('a*x^2 + b*x + c=0') or f2= sym('a*x^2 + b*x + c=0').  Note To create a symbolic expression that is a constant, you must use the sym command.  >> s_5 = sym('5'); s_5/3  % create a symbolic 5  ans =  5/3  >> a_5 = 5; a_5/3  % create a number 5  ans =  1.6667  If you set a variable equal to a symbolic expression, and then apply the syms command to the variable, MATLAB removes the previously defined expression from the variable.  >> syms a b;  >> f3 = a + b  %create a symbolic variable f3, whose value is a symbolic expression a+b  f3 =  a+b  >> syms f3  % removes the previously defined expression from the variable.  >> f3  f3=  f3  ² The subs Command You can substitute a numerical value for a symbolic variable using the subs command. to  >> f4 =’2*x^2 ­ 3*x + 1’ ;  %create a ‘char’ variable f4, equivalent to sym f4; …  f4=’2*x^2 ­ 3*x + 1’;  >> subs(f4,1) %substitute the value x = 1 in f4, i.e. 2*1^2­3*1+1  ans =  0  *Note To substitute a matrix  A into  the symbolic expression  f,  use the command  polyvalm(sym2poly(f),  A),  which replaces  all occurrences  of x  by A,  and replaces  the constant term of f with the constant times an identity matrix.  >> A=[1 2;3 4]  A =  1  2  3  4  >> polyvalm(sym2poly(f4), A)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有