Matlab Tutorial 1 David Goldstein University of Colorado Dept of Aerospace Engineering Sciences.

Slides:



Advertisements
Similar presentations
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
ECEN/MAE 3723 – Systems I MATLAB Lecture 1. Lecture Overview  Introduction and History  Matlab architecture  Operation basics  Visualization  Programming.
Intro MATLAB: Special Purpose Computer Program Optimized to perform engineering and scientific calculations Implements the MATLAB programming language.
MATLAB for Image Processing April 10 th, Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples.
CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Introduction to Matlab Jianguo Wang CSSCR September 2009.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
259 Lecture 15 Introduction to MATLAB. 2 What is MATLAB?  MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
1 Statistical Computing in MATLAB AMS 597 Ling Leng.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Basics of MATLAB By DR. Wafaa Shabana
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Introduction to MATLAB
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Matlab
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )
1 Matlab for Engineering Applications Introduced by Jen-Yang Chen Department of Electronic Engineering Ming Chuan University.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
An Introduction to Programming in Matlab Emily Blumenthal
Tutorial on Matlab Basics
Introduction to Matlab
Introduction to MATLAB
Prof. Mark Glauser Created by: David Marr
Computer Application in Engineering Design
Statistical Computing in MATLAB
Introduction to MATLAB
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
CS100J 26 April. Matlab Use help button!!! Variables, values, types
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Introduction to Matlab LAB 4
funCTIONs and Data Import/Export
INTRODUCTION TO MATLAB
How to Use MATLAB A Brief Introduction.
Presentation transcript:

Matlab Tutorial 1 David Goldstein University of Colorado Dept of Aerospace Engineering Sciences

Matlab Tutorial 2 Overview Help General –Scripts and Functions Vectors and Matrices Flow Control Plotting Loading and Saving Numerical Integration Example Symbolic Toolbox

Matlab Tutorial 3 Help demos help (list of help topics) help help (help on help) lookfor integration Documentation –helpdesk –../matlab/help/pdf_doc/matlab/using_ml.pdf –../ matlab/help/pdf_doc/matlab/getstart.pdf –../matlab/help/pdf_doc/matlab/ref/Refbook.pdf

Matlab Tutorial 4 General Two ways to use Matlab –Command Window acos(-1) or batch ; up arrow …, = space –Scripts and Functions Scripts and Functions called in command window Developed as text file with.m extension –help function or help script –Editor/Debugger –variables “declared” in function not available in work space –variables used in script are “global” variables

Matlab Tutorial 5 » help function FUNCTION Add new function. New functions may be added to MATLAB's vocabulary if they are expressed in terms of other existing functions. The commands and functions that comprise the new function must be put in a file whose name defines the name of the new function, with a filename extension of '.m'. At the top of the file must be a line that contains the syntax definition for the new function. For example, the existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector. The variables within the body of the function are all local variables. See SCRIPT for procedures that work globally on the work-space. incoming outgoing function name

Matlab Tutorial 6 » help script SCRIPT About MATLAB scripts and M-files. A SCRIPT file is an external file that contains a sequence of MATLAB statements. By typing the filename, subsequent MATLAB input is obtained from the file. SCRIPT files have a filename extension of ".m" and are often called "M-files". To make a SCRIPT file into a function, see FUNCTION. - Requires no input - Provides no explicit output - All variables used are available in the work space - Like a main program -.m file must be in your current path (help path) - path(path,a:\matlab)

Matlab Tutorial 7 Matrices help matfun help elmat useful commands: –eye(n) –ones(n) –zeros(n) –reshape(A,row,col) A=[2, 0; 0, 1; 3, 3] A =[A,[1;2;3]] A =(1:2,1:2)

Matlab Tutorial 8 » eye(2) ans = » ones(2) ans = 1 1 » zeros(2,1) ans = 0 » A = [2,0; 0,1; 3,3] A = » A =[A,[1;2;3]] A = » A(1:2,1:2) ans =

Matlab Tutorial 9 Vectors Special case of matrices –a = [ ] same as a = [0,1,2,3,4] –same as a = [0:4] –b = [0,.5, 1, 1.5, 2, 2.5] same as b = [0:.5:2.5] –c = [.1,.1,.1,.1,.1] same as c = ones(1,5) * 0.1 R = R(:) ensures R is a column vector 0.1 * a = a.*c = [0, 0.1, 0.2, 0.3, 0.4] (.* array multiply) a * c` (` is transpose) = 1.0

Matlab Tutorial 10 Flow Control ifelseifelseend switch caseotherwiseend forend while end break

Matlab Tutorial 11 if rem(n,2) ~= 0 M = odd_magic(n) elseif rem(n,4) ~= 0 M = single_even_magic(n) else M = double_even_magic(n) end switch (rem(n,4)==0) + (rem(n,2)==0) case 0 M = odd_magic(n) case 1 M = single_even_magic(n) case 2 M = double_even_magic(n) otherwise error(’This is impossible’) end

Matlab Tutorial 12 j = 1; for i = 0 : 0.1 : 5 t(j) = i; x(j) = exp(i); j = j+1; end while b–a > eps*b x = (a+b)/2; fx = x^3–2*x–5; if sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end t = [0:.1 : 5]; x = exp(t);

Matlab Tutorial 13 Plotting help plot plot(x,y)plot(x,y,’r’)plot3(x,y,z) axis([xmin xmax ymin ymax]) title(‘This is the title’) xlabel(‘x axis label’) ylabel(‘y axis label’) print -djpeg99 ‘d:\temp\plotexample.jpg’

Matlab Tutorial 14 » t = [0:.01:5]; » x = exp(t); » plot(t,x) » title(`e^t`) » xlabel(`time (seconds)`) » ylabel(`exp(t)`)

Matlab Tutorial 15 Saved as a.jpg (99)

Matlab Tutorial 16 Copied as Bitmap

Matlab Tutorial 17 Copied as Windows WMF

Matlab Tutorial 18 Loading and Saving Load and Save in binary and ASCII –ASCII easier to share with other applications save ‘d:\temp\my_matrix.txt’ A -ascii -double load ‘d:\temp\my_matrix.txt’ –becomes variable my_matrix –loads rows and columns of numbers only Be care in naming your variables save variable_name (saves all variables in the current workspace

Matlab Tutorial 19 » x = [0:10]`; » y = [x x.^2 x.^3 x.^4] y = » save `d:\temp\y1.txt` y -ascii

Matlab Tutorial e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e+004 Contents of y1.txt

Matlab Tutorial 21 Numerical Integration Let x(0) = 1 so, c = 0 function xdot = deriv(t,x) %deriv.m xdot = x; x0 = 1; [t, x] = ode45(‘deriv’, [0: 0.1: 5], x0) time vector Script function

Matlab Tutorial 22

Matlab Tutorial 23 Numerical Integration (cont.) Use Options to set the tolerance!!!!!!! Two-body Example:

Matlab Tutorial 24 Clear %Script to numerically integrate the 2-body equation global MU MU= e14; time = [0:20:10000] %Integration time in seconds R0 = [ ]; V0 = [ ]; x0 = [R0' V0']; tol = 1e-8; options = odeset('RelTol',tol,'AbsTol',[tol tol tol tol tol tol]); [t,x] = ode45('two_body', time, x0, options); clf subplot(2,1,1), plot(t,x(:,7),'r'), title('Radius vs Time'), ylabel('Rad Mag (m)'); subplot(2,1,2), plot(t,x(:,8),'b'), title('Velocity vs Time'), ylabel('Vel Mag (m/s)');

Matlab Tutorial 25 function xdot = two_body(t,x) %derivative function for numerical integrator % x(1) = Ri (Radius Vector in the i direction) % x(2) = Rj % x(3) = Rk % x(4) = xdot(1) = Vi (Velocity vector in the i direction) % x(5) = xdot(2) = Vj % x(6) = xdot(3) = Vk % xdot(4) = Ai (Acceleration vector in the i direction) % xdot(5) = Aj % xdot(6) = Ak MU = e14; r = norm(x(1:3)); xdot = [x(4); x(5); x(6); -MU * x(1) / r^3; -MU * x(2) / r^3; -MU * x(3) / r^3];

Matlab Tutorial 26 Symbolic Toolbox help symbolic Example syms x y z mu r = sqrt(x^2 + y^2 + z^2) U = mu/r acell_x = diff(U,x) acell_y = diff(U,y) acell_z = diff(U,z) r = (x^2+y^2+z^2)^(1/2) U = mu/(x^2+y^2+z^2)^(1/2) acell_x = -mu/(x^2+y^2+z^2)^(3/2)*x acell_y = -mu/(x^2+y^2+z^2)^(3/2)*y acell_z = -mu/(x^2+y^2+z^2)^(3/2)*z