 # Intro to Matlab programming

55 %
45 %
Information about Intro to Matlab programming
Education

Published on March 7, 2014

Source: slideshare.net

## Description

this is an powerful intro to MATLAB Programming.
I hope it to be useful to all of you :) .

MATLAB Programming By : Ahmed Moawad

LET’S BEGIN…… MATLAB is a tool that simplify the programming than any other programming language like c , c# , ……. Let’s begin with first function in MATLAB … And the most helpful Fn . >> help ; this function gives you a very useful tutorial , and information about using MATLAB in every applications. 1

FIRST TIME : HOW TO INPUT A MATRIX OR VECTOR IN MATLAB  Vector : >> x = [1 2 3 4] x=1 2 3 4 o Matrix : >> y = [1 2 3 ; 5 1 4 ; 2 3 1] y=1 2 3 5 1 4 2 3 1 2

CHARACTERISTICS OF MATRIX Transpose : >> xt = x ’ convert the columns to rows and vice versa x=1 2 3 4  >> x = [1 : 5] x=1 2 3 4 5 this is the step of increment  >>x = [1 : 2 : 5] x=1 3 5  3

CONTINUE…… >>size (y) Number of columns and rows  >>length(y) The tallest dimension(row , column) of Matrix 1  >>z = y( 2 , 2:3 ) Choose the second & third columns. 5 z=1 4 Choose the second row.  >>zeros(M ,N); Matrix consists of zeros  >>ones(M,N); Matrix consists of ones  >>eye(M,N) ; The main diameter of Matrix is ones , others zeros  >>rand (M,N) ; Uniform Distribution  >>randn (M,N); Normally Disribution  4

OPERATORS Scalar Arithmetic Operations : >> z = x * y; multiply every element in x to its corresponding in y.  Matrices Arithmetic Operations: >> z = x .* y ; multiply as matrices rules ,with condition (Num. of rows  of x = Num. of columns of y) + .+ Summation - .- Subtraction / ./ Division ^ .^ Exponential 5

FUNCTIONS BREAK (1):  >>sign(x) ; If an element in matrix x is +ve returns 1 ,-ve returns -1. >>exp (2) The exponential function. ans = 7.3891  round (x); Approximate the number x to the nearest number  Fix (x) ; Delete the fraction (1.2>>1, 1.6 >>1)  Abs(complex ); Magnitude of complex number  Angle(comp.); Angle of complex number  Real(complex); Real part of complex.  isprime(x) ; 0 for not , 1 for prime number  6

FLOW CONTROL FUNCTIONS For loop: i.e.: >>a=3; initial value of b >>for i = 1 :1: 10; initial : step : final a=a +i; end >> disp(a); Fn. that display the value of a >>sprintf (‘the number of icons is = %g ’ , a)  print the sentence ‘ ’ , replaces the %g by a 7

. CONTINUE … While loop: i.e.: >>b =0; initial value of b >>while b<4 carry out if b <4 , stop if else b=b+1; end >>weight = b*2.2; >>disp(b); >>sprintf(‘The weight equals %f ’ , weight)  weight is float type 8

.. CONTINUE … If Statement: i.e.:  degree =input(‘please insert the degree(0-100): ’) fn. that make user input the value which preferred if degree>= 85 disp(‘Excellent’) elseif( degree>= 75 & degree <85) disp(‘very good’) elseif( degree>= 65 & degree <75) disp(‘good’) elseif( degree>= 50 & degree <65) disp(‘pass’) else disp(‘fail’) end 9

… CONTINUE … Switch …case: i.e.: month= input (‘please input the month(1-12): ’) switch month case { 1,3,5,7,8,10,12} more than one case>> {1,2,…..} disp (31) case{4 , 6 ,9 ,11} disp (30) case 2 only one case>> 1 disp (28) end  10

FUNCTION BREAK(2): >>Str = ‘ the sentence you want to write’  >>w = str [ 1 2 5 7 9] w = thsne  x(2) = 4 change the second element value in the matrix x to 4.  X(3) = [ ] delete the third element .  mean(x) The mean of elements of x .  std(x) The standard diversion of elements of x.  [theta phi r ] = cart2sph [2 , 3 , 5]  any names indicates the sph. Conversion fn. the values of Cartesian coordinates 11

GRAPHS Line plot : i.e.: >>t = [ 0 : 10 ]; >> y = sin(t); >>Figure(1) >>plot (t,y) >>xlabel(‘time’) >>ylabel(‘input’) >>title (‘Gain’)  y is sinusoidal wave on t Open figure , name it 1 plot t(h-axis) versus y (v-axis) in figure. write 9time) under h-axis write (input) beside v-axis Make a title for plot 12

. CONTINUE … Bar Graph: i.e.: >>x = -3 : 1 : 3; >>y = x.^2; >>bar(x,y) Bar Graph >>figure(1) >>subplot 221 divide the figure into number of plots >>z= magic (3); special function >>subplot 222 row no. column no. position >>bar(z) >>subplot (2,2,3) >> bar(z , ‘grouped’) Style type >>subplot(2,2,4) >>bar (z , ‘stacked’)  13

.. CONTINUE … Histogram : >> hist (z , 7 )  Number of intervals in histogram Pie Graph : >>z = [10 4 5 8 2]; >>pie(z)  Polar Graph : >>polar(t , y)  14

… CONTINUE … Scatter plot : >>x =[1:10]; >> y = 2.*rand (1,10) >>subplot 221 >>scatter(x ,y) scatter points on plot >>subplot 222 >> stem ( x , y) scatter points connected with the h-axis >>subplot 223 >>scatter(x ,y , 3 , ‘y’ ) Mark color (yellow)  size of mark (scatter point) 15

…. CONTINUE … Pie graph scatter graph Polar graph Bar graph Histogram 16

FUNCTION BREAK(3) >>log (x)  >>log10(x)  >>log2 (x)  >>semilogy(x,y)  >>semilogx(x,y)  >>loglog(x,y)  >>barh(z)  >>break;  >>continue;  >>surf(x,y,z)  >>contour(x,y,z)  Ln Function Log. Decimal Function Log. Binary Function convert v-axis to dB convert h-axis to dB convert two axes to dB Opposite of Bar graph stop loop and move to the next command stop the itertion and move to the other itertion on loop Graph the surface and put the points on it 17

SYMBOLIC MATH :  Functions:  >>syms x y z ; convert this variables into symbols >>f = x^2 + y^2 +z^2; Substitution the symbols you want substitute >>f1 = subs( f , [x y] , [4 5] ) the values of substitution f1 = 16 + 25+z^2 Differentiation >>f2 = diff ( f , 2 , z ) The symbol you want to diff. f2 = 2 The order of diff. Integration >>f3 = int (f1 , -10 , 10 ) Final limit f3 = 5 initial limit Limit Fn >>limit ( sin (x) /x , inf ) the value the limit approximate to Summation >>symsum( 1 / x^2 , 1 , inf ) the final sub . of summation the initial sub. of summation               18

DOMAINS TRANSFORMS : Laplace Transform : >>syms t ; >> laplace ( cos (t)) laplace transform Ans = s / (s^2 +1) >>ilaplace (s/ (s^2 +1) inverse laplace transform Ans = cos t >>transferfn = tf ( , [1 2 1]) transfer fn. Transfer function = 1 / s^2 + 2* s + 1 >> bode(transferfn) Bode plot >> step(transferfn) Step response >> impulse (transferfn) Impulse response  19

. CONTINUE … Fourier Transform : >>syms t w b ; >>y = cos(b*t); >> f = fourier (x , t ,w ); >>y = cos( 2*pi *t); >>fd = fft ( y , 512 ); must be 2^n  20

THANK YOU

 User name: Comment:

## Related presentations #### Teaching Programs UAE | Teacher Training Courses U...

November 29, 2020 #### Teacher Training Courses UAE | Teaching Programs U...

November 29, 2020 #### OSCE in Nursing Education

November 29, 2020 #### Study In The University Of Central Lancashire

November 29, 2020 #### Модель взаимодействия с со...

November 29, 2020

## Related pages

### Introduction To MATLAB Programming | Mathematics | MIT ...

This course is intended to assist undergraduates with learning the basics of programming in general and programming MATLAB® in particular.

### Intro to MATLAB programming - scribd.com

Intro to MATLAB programming by zmahmood_25 ... Introduction to Programming. With Matlab J. Michael Fitzpatrick and John D. Crocetti

### Introduction to MATLAB Programming - The Department of ...

Introduction to MATLAB Programming. The goal of this tutorial is to introduce the fundamental ideas of programming in MATLAB. It requires no programming ...

### Lecture 1: Using MATLAB for the First Time | Videos ...

Introductory Programming; Life Sciences; Transportation . Translated Courses. ... Lecture 1: Using MATLAB for... Lecture 2: The Command Prompt. Lecture 3 ...

### Introduction to Computer Programming with MATLAB

Introduction to Computer Programming with MATLAB Aims. The aims of this short course are to introduce the elements and practicalities of computer ...

### INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS

INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS David Houcque Northwestern University (version 1.2, August 2005) ... 4 Introduction to programming in MATLAB 35

### Chapter 1: Introduction to MATLAB

Introduction to MATLAB ... some experience programming in another language, we hope that you can see how Matlab works by simply studying these programs.