Matlab simulink ppt

matlab programming ppt and how to plot multiple graphs in matlab
Dr.MattWood Profile Pic
Dr.MattWood,United States,Teacher
Published Date:25-07-2017
Your Website URL(Optional)
Comment
6.094 Introduction to programming in MATLAB Lecture 2: Visualization and Programming Danilo Šćepanović IAP 2010¾ User-defined Functions • Functions look exactly like scripts, but for ONE difference Functions must have a function declaration Help file Function declaration Outputs Inputs Courtesy of The MathWorks, Inc. Used with permission.User-defined Functions • Some comments about the function declaration Inputs must be specified function x, y, z = funName(in1, in2) Must have the reserved Function name should word: function match MATLAB file name If more than one output, must be in brackets • No need for return: MATLAB 'returns' the variables whose names match those in the function declaration • Variable scope: Any variables created within the function but not returned disappear after the function stops running¾¾¾¾ Functions: overloading • We're familiar with » zeros » size » length » sum • Look at the help file for size by typing » help size • The help file describes several ways to invoke the function D = SIZE(X) M,N = SIZE(X) M1,M2,M3,...,MN = SIZE(X) M = SIZE(X,DIM)¾¾ Functions: overloading • MATLAB functions are generally overloaded Can take a variable number of inputs Can return a variable number of outputs • What would the following commands return: » a=zeros(2,4,8); %n-dimensional matrices are OK » D=size(a) » m,n=size(a) » x,y,z=size(a) » m2=size(a,2) • You can overload your own functions by having variable input and output arguments (see varargin, nargin, varargout, nargout)Functions: Excercise • Write a function with the following declaration: function plotSin(f1) • In the function, plot a sin wave with frequency f1, on the sin f x () range 0,2π: 1 • To get good sampling, use 16 points per period. 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 5 6 7Functions: Excercise • Write a function with the following declaration: function plotSin(f1) • In the function, plot a sin wave with frequency f1, on the sin f x () range 0,2π: 1 • To get good sampling, use 16 points per period. • In an MATLAB file saved as plotSin.m, write the following: » function plotSin(f1) x=linspace(0,2pi,f116+1); figure plot(x,sin(f1x))¾¾¾¾¾¾¾¾¾¾¾¾ Relational Operators • MATLAB uses mostly standard relational operators equal == not equal = greater than less than greater or equal = less or equal = • Logical operators elementwise short-circuit (scalars) And &&& Or Not Xor xor All true all Any true any • Boolean values: zero is false, nonzero is true • See help . for a detailed list of operatorsif/else/elseif • Basic flow-control, common to all languages • MATLAB syntax is somewhat unique IF ELSE ELSEIF if cond if cond if cond1 commands commands1 commands1 end else elseif cond2 commands2 commands2 end else Conditional statement: evaluates to true or false commands3 end • No need for parentheses: command blocks are between reserved words¾¾¾¾ for • for loops: use for a known number of iterations • MATLAB syntax: Loop variable for n=1:100 commands end Command block • The loop variable Is defined as a vector Is a scalar within the command block Does not have to have consecutive values (but it's usually cleaner if they're consecutive) • The command block Anything between the for line and the end¾ while • The while is like a more general for loop: Don't need to know number of iterations WHILE while cond commands end • The command block will execute while the conditional expression is true • Beware of infinite loopsExercise: Conditionals • Modify your plotSin(f1) function to take two inputs: plotSin(f1,f2) • If the number of input arguments is 1, execute the plot command you wrote before. Otherwise, display the line 'Two inputs were given' • Hint: the number of input arguments are in the built-in variable narginExercise: Conditionals •Modify your plotSin(f1) function to take two inputs: plotSin(f1,f2) • If the number of input arguments is 1, execute the plot command you wrote before. Otherwise, display the line 'Two inputs were given' • Hint: the number of input arguments are in the built-in variable nargin » function plotSin(f1,f2) x=linspace(0,2pi,f116+1); figure if nargin == 1 plot(x,sin(f1x)); elseif nargin == 2 disp('Two inputs were given'); endOutline (1) Functions (2) Flow Control (3) Line Plots (4) Image/Surface Plots (5) VectorizationPlot Options • Can change the line color, marker style, and line style by adding a string argument » plot(x,y,’k.-’); color marker line-style • Can plot without connecting the dots by omitting line style argument » plot(x,y,’.’) • Look at help plot for a full list of colors, markers, and linestylesPlaying with the Plot to select lines and delete or change to see all plot properties tools at once to slide the plot to zoom in/out around Courtesy of The MathWorks, Inc. Used with permission.Line and Marker Options • Everything on a line can be customized » plot(x,y,'s','LineWidth',2,... 'Color', 1 0 0, ... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10) You can set colors by using 0.8 a vector of R G B values or a predefined color 0.6 character like 'g', 'k', etc. 0.4 0.2 0 • See doc line_props for a full list of -0.2 properties that can be specified -0.4 -0.6 -0.8 -4 -3 -2 -1 0 1 2 3 4Cartesian Plots • We have already seen the plot function » x=-pi:pi/100:pi; » y=cos(4x).sin(10x).exp(-abs(x)); » plot(x,y,'k-'); • The same syntax applies for semilog and loglog plots 50 10 » semilogx(x,y,'k'); 40 » semilogy(y,'r.-'); 10 » loglog(x,y); 30 10 20 10 • For example: » x=0:100; 10 10 » semilogy(x,exp(x),'k.-'); 0 10 0 10 20 30 40 50 60 70 80 90 1003D Line Plots • We can plot in 3 dimensions just as easily as in 2 » time=0:0.001:4pi; » x=sin(time); » y=cos(time); » z=time; » plot3(x,y,z,'k','LineWidth',2); » zlabel('Time'); 10 5 • Use tools on figure to rotate it 0 • Can set limits on all 3 axes -5 » xlim, ylim, zlim -10 1 0.5 1 0.5 0 0 -0.5 -0.5 -1 -1¾¾¾¾¾ Axis Modes • Built-in axis modes » axis square makes the current axis look like a box » axis tight fits axes to data » axis equal makes x and y scales the same » axis xy puts the origin in the bottom left corner (default for plots) » axis ij puts the origin in the top left corner (default for matrices/images)

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.