Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Notes on Solving Algebraic Equations Prof. R.G. Longoria October 2001/edits 10/2015 ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Matlab Use • These notes contain reference to using basic commands in Matlab, and writing m-files that run in Matlab. • You can also solve these problems using other software packages such as LabVIEW, MathCAD, or Mathematica, as well as some freeware packages (python, octave, etc.) • One issue in choosing a package is availability of examples and compatibility. • Another issue might be learning curve. Choose one that is easy for you to use. ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Solving Algebraic Equations • Solving Linear Algebraic Equations – Basic examples – Advanced Matrix Operations – Other Examples • Solving Nonlinear Algebraic Equations – fsolve – fzero – Newton ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Relevance of Solving Algebraic Equations • State space ODEs dx = f (x, t ) dt • Steady state and equilibrium problems dx = f ( x, t ) = 0 dt Linear Case Ax = b Solve with Gauss elimination Ill-conditioned cases Unique solution if it exists ME 383Q Modeling of Physical Systems Nonlinear Case f ( x) = 0 Solve with Newton method May have multiple solutions Iterative method Department of Mechanical Engineering The University of Texas at Austin Solving Linear Case in Matlab • Gauss elimination is implemented in the slash (/) and backslash (\) operators. xA = b » A = [2 3;6 7]; » b=[1 3]; » x = b/A x= 2.7500 -0.7500 Ax = b » b = [5;6]; » A\b ans = -4.2500 4.5000 ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Singular and Ill-Conditioned Matrices • Equations not independent: no unique solution • Inverse does not exist; singular matrix • Condition number (cond()) gives measure of how close to singular*; large cond implies illconditioned matrix *cond is normalized distance between matrix and nearest singular matrix. cond() is a 2-norm of the matrix. Rule of thumb: log10(cond(A)) is the number of significant digits you can expect to lose in Gaussian elimination. ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Example 1: Elastic system 5kx1 − 2kx2 = F − 2kx1 + 4kx2 = 2 F 5k − 2k − 2k x1 F = 4 k x2 2 F Ax = b » x = A\b » k = 1; F = 1; » A = [5*k -2*k;-2*k 4*k] » b = [F;2*F] A= b= 5 -2 -2 4 ME 383Q Modeling of Physical Systems 1 2 x= 0.5000 0.7500 Department of Mechanical Engineering The University of Texas at Austin Example 2: Resistive circuit In this circuit example, we are interested in the power delivered to R5. Two equations for loop currents. R1 + R2 + R3 − R3 Define A matrix in MATLAB: ME 383Q Modeling of Physical Systems − R3 i1 v1 − v2 = R3 + R4 + R5 i2 v2 − v3 A = [(R1+R2+R3) -R3;-R3 (R3+R4+R5)]; Department of Mechanical Engineering The University of Texas at Austin m-file Development • In this example, we want to vary one of the voltage sources, and compute the resulting power across resistor 5. • In the following m-file, note that this can be done with no for or while loops. All variation is implicit with the array addressing. ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin M-file % Modeling and Simulation with Matlab/Simulink % Exercise on Solving Linear Equations % % This file generates a solution for a linear circuit % problem (solves for currents). % A plot of the power delivered to resistor 5 is generated % as one of the voltage sources is varied. % % Written 9/9/99 (RGL). % Last revised: 2/27/2000. % % Define b array (here it turns into a matrix b = [E1-E2;E2-E3]; % Solve for currents, x = inv(A)*b % Note, make sure your dimensions match: % 2x2 * 2x21 => 2x21 (solution) x = A\b; % Now, extract the i2 current, which is x(2,:) in Matlab notation i2 = x(2,:); clear; help solve_circuit1 % Define Resistor Values R1=1;R2=1;R3=2;R4=2;R5=5; % Define fixed voltage sources E1 = 2; E3=5; % Compute P5 = R5*i2.*i2; OR P5 = R5*i2.^2 % Note, the dot before the ^ or * operator allows you % to operate on an element by element basis. P5 = R5*i2.*i2; % Plot P5 versus E2 % Define E2 as range of voltages E2 = 0:1:20; % Define A matrix A = [(R1+R2+R3) -R3;-R3 (R3+R4+R5)]; ME 383Q Modeling of Physical Systems plot(E2,P5) title('Power Delivered to Resistor 5 vs. E2') xlabel('E2, Volts') ylabel('Power, Watts') Department of Mechanical Engineering The University of Texas at Austin Plot of Power in R5 vs. v2 Power Delivered to Resistor 5 vs E2 3 2.5 Power, Watts 2 1.5 1 0.5 0 0 2 ME 383Q Modeling of Physical Systems 4 6 8 10 12 E2, Volts 14 16 18 20 Department of Mechanical Engineering The University of Texas at Austin Nonlinear Algebraic Equations • We need to solve the vector of functions: f ( x) = 0 • One-variable Newton’s method: fzero() • Example: Ginzburg eq λɺ = − x + λx 3 Create this function file, and solve with fzero() (roots?) ME 383Q Modeling of Physical Systems function f = ginzburg(x) global lambda f = -x+lambda*x.^3; Department of Mechanical Engineering The University of Texas at Austin Optimization Toolbox If our installation has the Optimization Toolbox, use the solver functions provided. Type at the command line: » help optim Optimization Toolbox. Version (will depend on your Matlab installation) (excerpt from many function listed) Nonlinear zero finding (equation solving). fzero - Scalar nonlinear zero finding. fsolve - Nonlinear system of equations solve (function solve). f ( x) = 0 ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin fsolve() » help fsolve fsolve Solves nonlinear equations by a least squares method. fsolve solves equations of the form: f(x)=0 where f and x may be vectors or matrices. x=fsolve(fun,x0) starts at the matrix x0 and tries to solve the equations described in ‘fun’. ‘fun’ is usually an m-file that returns an evaluation of the equations for a particular value of x: f=fun(x). Without Optimization Toolbox, you need to write your own code. The following slides explain one approach you’d need to take. ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Newton’s Method • Initial guess • Error δx = x1 − x* approx −1 δx = f ( x1 ) D ( x)1 Newton’s method is based on a Taylor expansion around the root f(x) solution x • New guess x1 • D is the Jacobian matrix x2 = x1 − δx = x1 − f ( x1 ) D −1 ( x1 ) ME 383Q Modeling of Physical Systems x2 Dij (x) ≡ x3 x0 ∂f j (x) ∂xi Department of Mechanical Engineering The University of Texas at Austin Function m-files • In creating a function m-file, note that you need to specify array indexing: • For example: for a function with 2 states function f = myfunction(x) f = [x(1)*x(2);x(1)+x(2)^2]; ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin Example: Nonlinear Mass-Spring m2 g f1 = −k1 ( X 1 − L1 ) + k 2 ( X 2 − L2 ) X2 Y X 22 − X 12 f 2 = k 2 ( X 2 − L2 ) − m2 g X2 k2 θ X1 X2 k1 m1 X1 A “calling routine” (m-file) globally defines these values function f = twodspring(x) global g m1 k1 L1 m2 k2 L2 f1 = -k1*(x(1)-L1)+k2*(x(2)-L2)*x(1)/x(2); f2 = -m2*g+k2*(x(2)-L2)*sqrt((x(2))^2-(x(1))^2)/(x(2)))]; f = [f1;f2]; ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin m-file calling fsolve() % This program solves a system of nonlinear equations % using the Matlab function: fsolve(). %%%%%%%%%%%%%%%%%%%%%%%%%%% % Equations are defined by function twodspring clear; help fstwodspring; % % Define constants needed for the two-dim. spring: % global g m1 k1 L1 m2 k2 L2 g = 9.81; % m1 = 0.1; % % Estimate k1: m1 deflects spring 10 mm %k1 = m1*g/0.01; k1 = 100; % L1 = 0.05; % m2 = 0.3; % ME 383Q Modeling of Physical Systems % k2 = 50; % L2 = 0.1; % x0 = [L1 L2]; Note: you must specify “guess” % % The following function calls the fsolve function. % Note: fsolve() is part of the Optimization Toolbox and not % a standard Matlab function. % x = fsolve('twodspring',x0) Department of Mechanical Engineering The University of Texas at Austin Newton’s method algorithm nsolve_twodk.m for MATLAB % x = [L1 L2]; a = 0; % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % The number of iterations before stopping is nsteps % (This can be replaced by a check on convergence.) % nsteps = 10; % Number of iterations before stopping % for icnt=1:nsteps, [f dfdx] = twodk(x,a); function [f,D] = twodk(x,a) dx = f/dfdx; % Function used by an n-variable Newton's method x = x - dx; % The variable a is a parameter (not used here) end global g m1 k1 L1 m2 k2 L2 % % Display Result f(1) = -k1*(x(1)-L1)+k2*(x(2)-L2)*x(1)/x(2); x f(2) = -m2*g+k2*(x(2)-L2)*sqrt((x(2))^2-(x(1))^2)/(x(2)); % Check Result % df(1)/dx(1) % flast should be close to zero D(1,1) = -k1+k2*(x(2)-L2)/x(2); [flast dfdx] = twodk(x,a); % df(1)/dx(2) flast twodk.m D(1,2) = k2*L2*x(1)/(x(2)*x(2)); % df(2)/dx(1) D(2,1) = -k2*(x(2)-L2)*x(1)/(x(2)*x(2)*sqrt(1-(x(1)/x(2))^2)); % df(2)/dx(2) D(2,2) = k2*(x(2)^3-L2*x(1)*x(1))/(x(2)^3*sqrt(1-(x(1)/x(2))^2)); return; ME 383Q Modeling of Physical Systems Department of Mechanical Engineering The University of Texas at Austin