Sunteți pe pagina 1din 41

MATLAB/SIMULINK

MATLAB

MATLAB is a powerful computing system for handling the calculations involved in scientific and engineering problems. The name MATLAB stands for MATrix LABoratory, because the system was designed to make matrix computations particularly easy.

MATLAB
One of the many things you will like about MATLAB (and which distinguishes it from many other computer programming systems, such as C++ and Java) is that you can use it interactively. This means you type some commands at the special MATLAB prompt, and get the answers immediately. like finding the solution to a system of differential equations.

The MATLAB desktop

Examples

2+3 <Enter> ezplot(tan(x))

Matrix
A matrix is a rectangular array of numbers. Row and column vectors, which we discussed above, are examples of matrices. Consider the 3 x 4 matrix

Vertical motion under gravity


If a stone is thrown vertically upward with an initial speed u, its vertical displacements after a time t has elapsed is given by the formula s=ut gt2/2, where g is the acceleration due to gravity. Air resistance has been ignored. We would like to compute the value of s over a period of about 12.3 seconds at intervals of 0.1 seconds, and to plot the distancetime graph over this period, The structure plan for this problem is as follows:
1. Assign the data (g, u and t) to MATLAB variables. 2. Calculate the value of s according to the formula. 3. Plot the graph of s against t. 4. Stop.

Program
% Vertical motion under gravity g = 9.8; % acceleration due to Gravity u = 60; % initial velocity (meters/sec) t = 0 : 0.1 : 12.3; % time in seconds s = u * t - g / 2 * t . 2; % vertical displacement in meters plot(t, s), title( Vertical motion under gravity ), ... xlabel( time ), ylabel( vertical displacement ),grid disp( [t s] ) % display a table

Applications

Image processing ANN Fuzzy logic Control FEM

What is Simulink
Simulink is a software package for modeling, simulating, and analyzing dynamical systems. It supports linear and nonlinear systems, modeled in continuous time, sampled time, or a hybrid of the two. Systems can also be multirate, i.e., have different parts that are sampled or updated at different rates.

Simulink

Simulink includes a comprehensive block library of sinks, sources, linear and nonlinear components, and connectors. You can also customize and create your own blocks.

Simulink
After you define a model, you can simulate it, using a choice of integration methods, either from the Simulink menus or by entering commands in MATLABs command window. The menus are particularly convenient for interactive work, while the command-line approach is very useful for running a batch of simulations (for example, if you are doing Monte Carlo simulations or want to sweep a parameter across a range of values).

Simulink

Using scopes and other display blocks, you can see the simulation results while the simulation is running. In addition, you can change parameters and immediately see what happens, for what if exploration. The simulation results can be put in the MATLAB workspace for postprocessing and visualization.

Simulink

And because MATLAB and Simulink are integrated, you can simulate, analyze, and revise your models in either environment at any point

Advantages

Previous simulation packages that require you to formulate differential equations and difference equations in a language or program For modeling, Simulink provides a graphical user interface (GUI) for building models as block diagrams, using click-and-drag mouse operations

Example: Sine Wave

The Sine Wave block provides a sinusoid. The block can continuous or discrete mode. The output of the Sine Wave block is determined by:
y = Amplitude x sin(frequency x time + phase)

Sine Wave
Simulink runs under Matlab. First start Matlab, then type simulink at the Matlab prompt. This is a library of blocks that are available for putting into the Simulink block diagram.

Simulink Help
Select Simulink Help from the help menu in the library browser. Here you can find tutorials, demos, information

on vailable
blocks, and so on.

Sine Wave
A Simulink model is a block diagram. Click File|New|Model in the Library Browser. An empty block diagram will pop up. You can drag blocks into the diagram from the library.

Sources: Produce Signals

Select sources from the library. Drag any block you want to use into the model.

Sinks: Terminate Signals

Select sinks from the library. Drag any block you want to use into the model.

Connecting Blocks Drag a signal line from the output of a block to the input of mnother block. Ctrl-Click will automatically connect.

Running the Simulation


Change parameters under Simulation Configuration Parameters.

Running the Simulation

Set start and stop time for the simulation here.

Note: numerical solution using ode45!


I often change from variable-step to fixedstep to get a smootherlooking solution.

Viewing Results: Scope


You can save data from the scope to the workspace using the Parameters, Data History tab.

Modifying Block Properties

Double click on any block to bring up a properties box. Here are the sine wave properties.

How Simulink Works

Each block within a Simulink model has these general characteristics: a vector of inputs, u, a vector of outputs, y, and a vector of states, x:
x

u (input)

(States)

y (output)

Simulation
Simulation consists of two phases: initialization and simulation. During the initialization phase:
The block parameters are passed to MATLAB for evaluation. The resulting numerical values are used as the actual block parameters. The model hierarchy is flattened. Each subsystem that is not a conditionally executed subsystem is replaced by the blocks it contains.

Simulation
Blocks are sorted into the order in which they need to be updated. The sorting algorithm constructs a list such that any block with direct feed through is not updated until the blocks driving its inputs are updated. It is during this step that algebraic loops are detected. The connections between blocks are checked to ensure that the vector length of the output of each block is the same as the input expected by the blocks it drives.

Simulation
A model is simulated using numerical integration. Each of the supplied ODE solvers (simulation methods) depends on the ability of the model to provide the derivatives of its continuous states. Calculating these derivatives is a two-step process. First, each blocks output is calculated in the order determined during the sorting. Then, in a second pass, each block calculates its derivatives based on the current time, its inputs, and its states. The resulting derivative vector is returned to the solver, which uses it to compute a new state vector at the next time point. Once a new state vector is calculated, the sampled data blocks and Scope blocks are updated.

The Simulink Real-Time Workshop

Rapid Prototyping. As a rapid prototyping tool, the Real-Time Workshop enables you to implement your designs quickly without lengthy hand coding and debugging. Embedded Real-Time Control. Once a system has been designed with Simulink, code for real-time controllers or digital signal processors can be generated, crosscompiled, linked, and downloaded onto your selected target processor

The Simulink Real-Time Workshop

Real-Time Simulation. You can create and execute code for an entire system or specified subsystems for hardwarein-the-loop simulations. Typical applications include training simulators (pilot-in-the-loop), real-time model validation, and testing

Exercise:Converting Celsius to Fahrenheit

To model the equation that converts Celsius temperature to Fahrenheit:


TF = 9/5(TC) + 32

Exercise:Converting Celsius to Fahrenheit First, consider the blocks needed to build the model:
A Ramp block to input the temperature signal, from the Sources library A Constant block, to define a constant of 32, also from the Sources library A Gain block, to multiply the input signal by 9/5, from the Math library A Sum block, to add the two quantities, also from the Math library A Scope block to display the output, from the Sinks library

Model
Assign parameter values to the Gain and Constant blocks by opening (double-clicking on) each block and entering the appropriate value. Then, click on the Close button to apply the value and close the dialog box. Now, connect the blocks.

Results
The Ramp block inputs Celsius temperature. Open that block and change the Initial output parameter to 0. The Gain block multiplies that temperature by the constant 9/5. The Sum block adds the value 32 to the result and outputs the Fahrenheit temperature. Open the Scope block to view the output. Now, choose Start from the Simulation menu to run the simulation. The simulation will run for 10 seconds.

SimMechanics

Ground

Joint 1

Arm 1

Joint 2

Arm 2

S-ar putea să vă placă și