Attribution Non-Commercial (BY-NC)

11 views

Matlab Intro

Attribution Non-Commercial (BY-NC)

- Clustering. Computational journalism week 2
- Dynamic of structures
- 19790019360_1979019360_NASTRAN_Theoretical_Manual_16.0_1978
- FEM QUESTIONS
- ECE Formula
- TIFR 2013 QUESTION PPR
- Dynamic characteristics of a curved cable-stayed bridge from strong motion records
- engdynamics2011.0v1.pdf
- The Capacity Region of the Gaussian Multiple-Input Multiple-Output Broadcast Channel_Weingarten & Steinberg & Shamai_IEEE Tran., Vol. 52, No. 9, Sept. 2006.pdf
- BAUPC_sols04
- Jordan Form
- Nonlinear Principal Component Analysis and Related Techniques
- Final Project m.tech 2016-1 Copy File 3
- A Unified Approach to Substructuring and Structural Modification Problems
- Eigenvalue problems.pdf
- RC1901A24 Term Paper
- 2005s
- pr_l3
- Harmonic Resonance Mode Analysis (1).pdf
- Imac 96 Id

You are on page 1of 19

MATLAB

Built in constants and variables ans eps i or j inf pi most recent answer small constant ~ 10-16 imaginary unit infinity 3.14159 ... Some common functions sin(x) cos(x) exp(x) sqrt(x) log(x) log10(x) abs(x) sine cosine exponential square root natural log log to base 10 absolute value, magnitude of complex quantity angle(x) phase angle real(x) real part of imag(x) imaginary part of

Standard mathematical operations * / ^ addition, subtraction multiplication division exponentiation e.g. yn = y^n

>> x= x = 0:0.1:0.5 0 0.1000 0.2000 0.3000 0.4000 0.5000 >> y= y =linspace(0, 0.5, 4) 0 0.1667 0.3333 0.5000 >> z= 1 z = [ 1 2 3 4 5] 2 3 4 5

>> >> >> z= 1 z = [ 1 2 3 4 5] ; z 2 3 4 5

Comment line

>> % This is a comment >>

>> x = [ 1 2 3 4 5]; >> y = exp(x) y= 2.7183 7.3891 20.0855 54.5982 148.4132

>> x= 1 >> y= 3 >> z= 1 >> f= 1

x = [ 1 2 3 4] 2 3 4 y=x+2 5 6 z = x .^2 9 16 f = x .*x .^2 27 64

>> z = 3 + 4*i z= 3.0000+ 4.0000i >> y =i*z y= -4.0000+ 3.0000i >> x =[ 1 + 3*i 2*i] x= 1.0000+ 3.0000i 0+ 2.0000i >>y = [ i i] y= 0+ 1.0000i 0+ 1.0000i >> z = x .*y z= -3.0000+ 1.0000i -2.0000

>> ans = -1 >> >> ans = 1.0000 exp(i*pi)

pi] ;

-1.0000

Simple plotting

>> >> >> x = linspace( 0, 2*pi, 100); y = cos(x); plot(x, y)

1

-1

or

>> >> >> >> >> >> >> x = linspace(0, 2*pi, 100); y1= cos(x); plot(x, y1) hold on y2 = sin(x); plot(x, y2) hold off

>> >> >> >> >> >> x = linspace( 0, 2*pi, 100); y = cos(x); plot(x, y) xlabel(' x-axis text here') ylabel(' y axis text here') title('title text here')

>> >> >> >> >> x = linspace( 0, 2*pi, 100); y1 = cos(x); y2 = cos(x+1); y3 = cos(x+2); plot(x, y1, '--',x,y2,' :',x, y3,'.-' )

y1 dashed line

y2 dotted line

y3 dot-dash

>> >> ans = 0 0 >> ans = 1 1 x = [ 1 2 3 4 5]; x >3 0 1 1 x <= 4 1 1 0

>> >> >> x = linspace(0, 1, 500); y =(x .^2).*(x < 0.5) + (0.75 - x ).*(x >= 0.5); plot(x, y)

0.25 0.2 0.15 0.1 0.05 0 -0.05 -0.1 -0.15 -0.2 -0.25

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

>> x = [ 5 6 3 8 7 9]; >> x(1:3) ans = 5 6 3 >> x(2:4) ans = 6 3 8 >> x(3:end) ans = 3 8 7 9

>> >> ans = 5 >> ans = 2 x = [3 4]; norm(x)

length(x)

>> >> x=linspace(0, 4, 100); y=sin(x)./x;

1.2

Warning: Divide by zero. >> >> >> x = x + eps*(x = = 0); y=sin(x)./x; plot(x,y)

0.8

0.6

0.4

0.2

-0.2

0.5

1.5

2.5

3.5

function y = infinitytest(x) x = x + eps*( x = = 0); y = 3/(1 + 4/x); >> infinitytest(1) ans = 0.6000 >> infinitytest(0) ans = 1.6653e-16 >> infinitytest(inf) ans = 3

MATLAB Functions Functions are defined in the editor and saved as M-files. The name of the M-file is the name of the function with a .m extension, e.g. myfunction.m etc. As shown in the example below, functions can have multiple outputs (as well as multiple inputs). function [y , z] = test(x) y = x.^2; z = 10*exp(-x);

10 9 8 7 6 5 4 3 2 1 0

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

All the variables appearing in a function are local to that function, i.e. they do not change any similarly named variables in the MATLAB workspace.

MATLAB Scripts A MATLAB script is a sequence of ordinary MATLAB statements, defined in the editor and then saved as a file.The file has the name of the script followed by a .m extension, e.g. myscript.m. Typing the script name at the MATLAB prompt then causes the script to be executed. Variables in the script change any values of variables of the same name that exist in the current MATLAB session.

% testscript x =linspace(0,2,100); y = x.^2; z = 10*exp(-x); plot(x,y,x,z) >> testscript

10 9 8 7 6 5 4 3 2 1 0

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

MATLAB Matrices MATLAB was designed to perform operations with matrices very effectively. Entering matrices manually is as easy as vectors:

>> matrix = [ 4 0 3; 0 3 5; 357 ] 4 0 3 0 3 5 3 5 7

matrix =

>> ans = 5 matrix(2,3)

>>matrix(: , 2) ans = 0 3 5 >>matrix(3, :) ans = 3 5 7

>> ans = 3 3 >> ans = 14 >> ans = -43 >> ans = 4 0 3 0 3 5 3 5 7

returns number of rows, nr, and number of columns, nc, as a vector [nr , nc] trace of M (sum of diagonal terms) determinant of M transpose of M (interchange rows and columns)

size(matrix)

trace(matrix)

det(matrix)

matrix'

>> >> ans = 13 >> vt = 1 2 3 >> ans = 13 21 34 >> ans = 157 matrix*vt 21 v = [ 1 2 3]; v*matrix

34 vt = v'

v*matrix*vt

Special matrices zeros(m,n) ones(m,n) eye(m,n) matrix of all zeros with m rows and n columns matrix of all ones with m rows and n columns identity matrix with m rows and n columns

1 1 1

0 1 0

>> M =[ 1 3 5; 2 1 1; 4 3 6] M= 1 3 5 2 1 1 4 3 6 >> b =[3; 2; 1] b= 3 2 1 >> x= -0.0909 3.9091 -1.7273 x =M\b

Determining the eigenvectors and eigenvalues of a matrix M (For a real symmetric matrix the eigenvalues are real and the eigenvectors are real and orthogonal to each other) [eigenvects, eigenvals] =eig(M)

>> evects =

-0.8752 0.3507 0.3332 0.4783 0.7300 0.4881 0.0720 -0.5865 0.8067 evals = 3.7531 0 0 0 -1.0172 0 0 0 11.2641

Corresponding eigenvalues

- Clustering. Computational journalism week 2Uploaded byJonathan Stray
- Dynamic of structuresUploaded byMauricio Bahamon
- 19790019360_1979019360_NASTRAN_Theoretical_Manual_16.0_1978Uploaded byjuillenl
- FEM QUESTIONSUploaded bymadream7
- ECE FormulaUploaded byJignesh Detroja
- TIFR 2013 QUESTION PPRUploaded bySundeep Chopra
- Dynamic characteristics of a curved cable-stayed bridge from strong motion recordsUploaded byDionysius Siringoringo
- engdynamics2011.0v1.pdfUploaded bysteefff88
- The Capacity Region of the Gaussian Multiple-Input Multiple-Output Broadcast Channel_Weingarten & Steinberg & Shamai_IEEE Tran., Vol. 52, No. 9, Sept. 2006.pdfUploaded bySinshaw Bekele
- BAUPC_sols04Uploaded byFadli Viensfise Sebelasipalima
- Jordan FormUploaded bymichel_faleiros
- Nonlinear Principal Component Analysis and Related TechniquesUploaded byreginaxy
- Final Project m.tech 2016-1 Copy File 3Uploaded byajay
- A Unified Approach to Substructuring and Structural Modification ProblemsUploaded byDanilo Álvaro
- Eigenvalue problems.pdfUploaded byEva Dokic
- RC1901A24 Term PaperUploaded byRashesh Bhatia
- 2005sUploaded byKagz
- pr_l3Uploaded byBirkan Göçerler
- Harmonic Resonance Mode Analysis (1).pdfUploaded byJorge Campos
- Imac 96 IdUploaded byasimaliabbasi
- Summary for LinearUploaded byGizem Daban
- block_triang_reductionUploaded bySyed Haider Jawad Abidi
- Chap2 ThomasUploaded byLdTorrez Ldtc
- CHAPTER 2 UpdatedUploaded byMuhammad Mursyid
- Sols 02Uploaded byDavid Lee
- Concrete Finite Element Analysis - Chapter 5Uploaded byjorgebulk7331
- MidtermUploaded byHassanBakar
- Improved Penalization for Determining the Number of Factors in Approximate Factor ModelsUploaded byJaime Sanchez
- Active Antenna Selection in Multiuser MIMO CommunicationUploaded byNadisanka Rupasinghe
- ijcttjournal-v1i1p13Uploaded bysurendiran123

- Gear Oil ClassificationUploaded bySiva Reddy
- Transaxle OilUploaded bySunilkumar Reddy
- 16.pdfUploaded bySunilkumar Reddy
- Brakes PrintableUploaded bySunilkumar Reddy
- Hydraulic Hoses StandardsUploaded bysaliyarumesh2292
- BUCKET CAPACITYUploaded byArjun JP
- Steer Susp FundUploaded bySunilkumar Reddy
- antilock_brakes.pdfUploaded bySunilkumar Reddy
- hfh_ch03Uploaded bysamik4u
- DT Stoerkle RestrictedUploaded bySunilkumar Reddy
- Golenko_Fundamentals of Machine DesignUploaded byDarwin Lim
- Solutions ManualUploaded bySunilkumar Reddy
- cutting.pdfUploaded bySunilkumar Reddy
- Wubneh_Abiy_Spring+2011 (1)Uploaded bySunilkumar Reddy
- 00643Uploaded bySunilkumar Reddy
- Hydraulic Hose SelectionUploaded bydavo87
- k4441Uploaded bySunilkumar Reddy
- Heat Transfer SoundUploaded byme kon hu
- Autex - 4Uploaded bySunilkumar Reddy
- A 0582112113Uploaded bySunilkumar Reddy
- 9005Uploaded bySunilkumar Reddy
- Colin PeacheyUploaded bySunilkumar Reddy
- cibrarioUploaded bySunilkumar Reddy
- 13342Uploaded bySunilkumar Reddy
- 13341Uploaded bySunilkumar Reddy
- 1-s2.0-S1877705813018948-mainUploaded bySunilkumar Reddy

- Hellenistic Philosophy and Some ScienceUploaded byXristina Bartzou
- Rsh Qam11 Excel and Excel QM ExplsM2010Uploaded byhlgonzalez
- Tok Sb Ibdip Ch6 (Mathematics)Uploaded bytashamapenzi
- Ch 3 Vector CalculusUploaded byHemal Parikh
- BooklistUploaded bykeelor
- Find Minimum of Unconstrained Multivariable Function Using Derivative-free Method - MATLAB FminsearchUploaded byCristian Giovanny Gomez Olarte
- Comparison of Correlation Dimension and FractalUploaded byChristian F. Vega
- ByjU BookletUploaded byRishiraj Nalandi
- Pingree, Steele, Burnett - Pathways into the Study of Ancient Sciences.pdfUploaded byManticora Pretiosa
- A i SolutionsUploaded bystudent1291
- 7830Uploaded byNazir Rashid
- Al II H Final Review PDFUploaded bysanthi
- Design Optimization of Mechanical ComponentsUploaded byVeki77
- Fourier Series and SpectraUploaded byHarshit Goyal
- Rancob PhiaUploaded byHans Kristian Akar
- Advanced Microeconomics – Assignment 1Uploaded byrethmlai7516
- The Magnetic Field Produced at a Focus of a Current carrying conductor in shape of a conic sectionUploaded byArchisman Panigrahi
- (0) Algebra Distributive 2terms Some Exponents AllUploaded byNurul
- Number SystemsUploaded bychand7790
- Cgt NotesUploaded byhgfsjkre
- Synthetic Analytic MethodUploaded byPooja Chavande-Suradkar
- logik4Uploaded bypoptard
- Fluent 6_0 Tuturial Guide_Volume 5Uploaded byPedro Lisboa
- 700001244_Topper_8_101_2_3_Mathematics_2015_questions_up201506182058_1434641282_7607Uploaded byKareena Gupta
- 0580_w13_ms_22.pdfUploaded byHaider Ali
- Em 4e 2011prelim Catholichighsch 2011 Prelim III Maths 4016 1Uploaded bylinnbrownie
- hwassignmentUploaded byAditya Kumar
- Time Series Analysis of Bus speeds in DelhiUploaded byM Mushtaq
- Fitting DistributionsUploaded byParikshit Yadav
- W2- Cost IndexUploaded bySarah Mazhar Iqbal Khan