1 / 38

Introduction to MATLAB 國家太空計畫室 劉修任 October 21, 2004

Introduction to MATLAB 國家太空計畫室 劉修任 October 21, 2004. MATLAB Desktop. Running Functions and Entering Variables. The prompt (>>) in the Command Window indicates that MATLAB is ready to accept input from you.

Download Presentation

Introduction to MATLAB 國家太空計畫室 劉修任 October 21, 2004

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to MATLAB 國家太空計畫室 劉修任 October 21, 2004

  2. MATLAB Desktop

  3. Running Functions and Entering Variables • The prompt (>>) in the Command Window indicates that MATLAB is ready to accept input from you. • To enter a variable, type the variable with its assignment. For example, type A = [1 2 3; 4 5 6; 7 8 10] , then MATLAB responds with A = 1 2 3 4 5 6 7 8 10 • To run a function, type the function including all arguments. For example, type magic(2), then MATLAB returns ans = 1 3 4 2

  4. Variable Types • Local Variables Each MATLAB function has its own local variables. These are separate from those of other functions, and from those of the base workspace. Variables defined in a function do not remain in memory from one function call to the next, unless they are defined as global or persistent. • Global Variables If several functions, all declare a particular name as global, then they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the other functions declaring it global.

  5. Variable Types (Cont.) • Persistent Variables Characteristics of persistent variables are : • They differ from global variables in that persistent variables are known only to the function in which they are declared.This prevents persistent variables from being changed by other functions or from the MATLAB command line • You can only use them in functions, other functions are not allowed access to them.

  6. Variable Types (Cont.) • MATLAB does not clear them from memory when the function exits, so their value is retained from one function call to the next. • If you clear the function or edit the M-file for that function, then MATLAB clears all persistent variables used in that function. • If the persistent variable does not exist the first time you issue the PERSISTENT statement, it will be initialized to the empty matrix.

  7. Opening, Loading, Saving Files

  8. Low-Level File I/O

  9. Arithmetic Operators Example:

  10. Scripts • Simple Script Example • These statements calculate rho for several trigonometric functions of theta, then create a series of polar plots. • % An M-file script to produce % Comment lines • % "flower petal" plots • theta = -pi:0.01:pi; % Computations • rho(1,:) = 2*sin(5*theta).^2; • rho(2,:) = cos(10*theta).^3; • rho(3,:) = sin(theta).^2; • rho(4,:) = 5*cos(3.5*theta).^3; • for k = 1:4 • polar(theta,rho(k,:)) % Graphics output • pause • end

  11. Scripts (Cont.) • Entering the commands in an M-file called petals.m (shown as Figure A). Typing petals at the MATLAB command line executes the statements in the script (shown as Figure B). Figure A Figure B • Results for the script executed: : After the script displays a plot, press “Enter” to move to the next plot.

  12. Function • Simple Function Example • The average function is a simple M-file that calculates the average of the elements in a vector. • function y = average(x) • % AVERAGE Mean of vector elements. • % AVERAGE(X), where X is a vector, is the mean of vector elements. • % Nonvector input results in an error. • [m,n] = size(x); • if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) • error('Input must be a vector') • end • y = sum(x)/length(x); % Actual computation

  13. Function (Cont.) • Entering these commands in an M-file called average.m. • To call the average function, enter • z = 1:99 • average(z) • ans = • 50

  14. Flow Control • There are eight flow control statements in MATLAB: • if, together with else and elseif …end, executes a group of statements based on some logical condition. • if logical_expression • statements • elseif logical_expression • statements • else • statements • end • switch, together with case and otherwise, executes different groups of statements depending on the value of some logical condition. • switch expression (scalar or string) • case value1 • statements % Executes if expression is value1 • case value2 • statements % Executes if expression is value2 • . . • otherwise • statements % Executes if expression does not match any case • end • while executes a group of statements an indefinite number of times, based on some logical condition. • while expression • statements • end

  15. Flow Control (Cont.) • for executes a group of statements a fixed number of times. • for index = start:increment:end • statements • end • continue passes control to the next iteration of a for or while loop, skipping any remaining statements in the body of the loop. • break terminates execution of a for or while loop. • try...catch…end changes flow control if an error is detected during execution. • try, • statement, • ..., • statement, • catch, • statement, • ..., • statement, • end • return causes execution to return to the invoking function. For example: try X = A * B catch disp '** Error multiplying A * B' end

  16. Basic Plotting Commands • plot: Graph 2-D data with linear scales for both axes • plot3: Graph 3-D data with linear scales for both axesloglogGraph with logarithmic scales for both axes • semilogx: Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis • semilogy: Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis • plotyy: Graph with y-tick labels on the left and right side For example: • x = 0:0.01:20; • y1 = 200*exp(-0.05*x).*sin(x); • y2 = 0.8*exp(-0.5*x).*sin(10*x); • [AX,H1,H2] = plotyy(x,y1,x,y2,'plot');

  17. Building Models • Linear Models--Building single-input, single-output (SISO) models of linear time-invariant (LTI) dynamical systems, including state-space, pole/zero, and transfer functions. • State-space (SS) models: sys_ss = ss(a,b,c,d) For example: A = [1 0; 0 1]; B = [1; 0]; C = [0 1]; D = [0]; sys_ss = ss(A,B,C,D) • Transfer functions (TF) model: sys_tf = tf(num,den) For example: sys_tf = tf(1.5,[1 14 40.02])

  18. Building Models Building Models (Cont.) • Zero-pole-gain (ZPK) model: sys_zpk = zpk(z,p,k) For example: sys_zpk = zpk([],[-9.996 -4.004], 1.5) • Discrete-Time Models-- Creating discrete-time models is very much like creating continuous-time models, except that you must also specify a sampling period or sample time for discrete-time models. • sys1 = tf(num,den,Ts) • sys2 = zpk(z,p,k,Ts) • sys3 = ss(a,b,c,d,Ts)

  19. sys u sys1 sys2 y sys sys1 + y u + sys2 + u sys1 y - sys2 Interconnecting Linear Models • Series connection of two LTI (Linear Time-Invariant) models: sys = series(sys1,sys2) • Parallel connection of two LTI models: sys = parallel(sys1,sys2) • Feedback connection of two LTI models: sys = feedback(sys1,sys2,-1)

  20. Common Control Functions • Model Dynamics • Time Responses

  21. Common Control Functions (Cont.) • Frequency Response

  22. Starting Simulink Click the Simulink icon on the MATLAB toolbar, or enter the simulink command at the MATLAB prompt

  23. Creating a New Model

  24. Creating a New Model (Cont.)

  25. Connecting Blocks & Their Parameters Setting

  26. Running a Simulation • Running a Simulink model is generally a two-step process: • Specify various simulation parameters (either on the Simulation Parameters dialog box, from the model editor's Simulation menu or using simset or set_param commands. • Start the simulation • To start execution of a model, select Start from the model editor's Simulation menu or click the Start button on the model's toolbar. You can also use the keyboard shortcut, Ctrl+T, to start the simulation.

  27. Modeling a Simple Continuous System • To model the differential equation • where u(t) is a square wave with an amplitude of 1 and a frequency of 1 rad/sec. Start simulation icon P.S. Click the Start simulation icon on the MATLAB toolbar, or enter the sim(‘example_1’) command at the MATLAB prompt

  28. Modeling a Second-Order Control System To model a second-order plant and the controller into a closed-loop system, and then to simulate its step response.

  29. Simple S/C Control Example

  30. Simple S/C Control Example (Cont.) -- Attitude Control Block Diagrm 太空環境 (Environment) Implemented by Software ADCS Hardware Disturbance 1.命令:衛星姿態 2.命令:衛星角速度 3.命令:衛星角加速度 控制器 (Controller) 致動器 (Actuators) 衛星動態 & 衛星軌道 濾波器 (Compensator) 1. 衛星姿態 (Quaternion) 2. 衛星角速度 (Angular Velocity) 估測器 (Estimator) 感測器 (Sensors) 1. 迴授訊號:衛星姿態 2. 迴授訊號:衛星角速度 General Structure of a Satellite Attitude Determination and Control Subsystem

  31. Dynamic Equation I: Spacecraft moment of inertia N: External torque (Thruster & Environment) Hw: Wheel angular momentum w: Spacecraft angular velocity Kinematical Equation Q: Spacecraft quaternion Simple S/C Control Example (Cont.) -- Dynamic & Kinematic Equations

  32. Simple S/C Control Example (Cont.) -- Block: Dynamic & Kinematic Eq.

  33. Simple S/C Control Example (Cont.) -- Block: PD Controller & Timer

  34. Simple S/C Control Example (Cont.) -- Block: Data Display

  35. Simple S/C Control Example (Cont.) -- Simulation Results

  36. MATLAB -- Help

  37. Plot the root locus using MATLAB for the open-loop system shown in the figure with , and . If K = 3.7, plot the step response. Homework I y r + -

  38. K B M x(t) f(t) Homework II Consider the simple mechanical system of the figure. Three forces influence the motion of the mass, namely, the applied force, the frictional force, and spring force. Applying Newton’s low of motion, the force equation of the system is With the system initially at rest, a force of 25 Newton is applied at time t = 0. Assume that the mass M = 1kg, friction coefficient B = 5N/m/sec., and the spring constant K = 25 N/m. Plot the time response of the displacement and velocity of the system.

More Related