You are on page 1of 21

1

Programming In MATLAB

Imtiaz Hussain Kalwar


imtiaz.kalwar@gmail.com
imtiaz.kalwar@googlepages.com
Contents
Basic Commands

• ummation
• Taylor Series
• Substitution and simplification
Taylor Series Calculator
• Differential Calculus
• Integral Calculus
• Limits

Function Calculator
• Laplace Transform
• Fourier Transform
• Z-Transform
• And their respective inverses
3
Basic Commands

4
Input Command

• input Prompt for user input.

• R = input('How many apples') gives the user the prompt in the


text string and then waits for input from the keyboard.

• R =input('What is your name','s') gives the prompt in the text


string and waits for character string input.

5
Input Command

For Example when we will execute following command

R = input(‘How many apples = ‘)

Matlab Returns:

How many apples = 40

R=

40
6
Pause Command

• pause Wait for user response.

• pause(n) pauses for n seconds before continuing, where n can


also be a fraction.

• pause causes a procedure to stop and wait for the user to strike
any key before continuing.

7
Pause Command

For Example when we will execute following command

R = input(‘How many apples = ‘)

pause

S=input(‘How many Mangos = ’)

8
Pause Command

For Example when we will execute following command

R = input(‘How many apples = ‘)

pause(3)

S=input(‘How many Mangos = ’)

9
Keyboard Command
• keyboard, when placed in an M-file, stops execution of the file
and gives control to the user's keyboard

• The special status is indicated by a K appearing before the


prompt.

• The keyboard mode is terminated by executing the command


return.

10
Keyboard Command

For Example when we will execute following command

R = input(‘How many apples = ‘)

keyboard

S=input(‘How many Mangos = ’)

11
Display Command

• disp(‘string’) displays the string on the screen.

>> disp(‘Who are you?’)

Who are you?

12
Display Command
• disp(x) displays the variable value on the screen without
printing its name.

>> a=3;
>> disp(a)
3

13
sprintf Command
• sprintf(‘%g’,x) displays the variable value on the screen.
• sprintf(‘ the value is %g’,x) displays the variable value on
the screen.

>> a=3;
>> sprintf(‘ the value of a is = %g’, a)
the value of a is = 3

14
Exercise#1
1. Write down MATLAB code that accepts resistor values in following
manner.
– Enter number of 5k resistors
– Enter number of 10k resistors
– Enter number of 13K resistors

2. After entering the values wait for 5sec and again accepts following
values.
– Enter number of Si diodes
– Enter number of Ge diodes
– Enter number of GaAs diodes

15
Exercise#1 (contd…)
3. After Storing the values computer displays all the entered values one
by one by pressing the key from key board .
4. Display total number of resistors.
5. Display total number of diodes.
6. Display Total number of Components.
7. Display how much percent the resistors are among all the
components.
8. Display how much percent the diodes are among all the components.

16
Exercise#1 (contd…)
9. Modify your program in such a way that if user enters wrong
number of components he/she can correct it later on.

17
Flow Control

18
Flow Control
• MATLAB has several flow control constructs:
– if
– switch and case
– For
– while
– Continue
– Break
– try – catch
– return

19
IF-Statement

• The if statement evaluates a logical expression and executes a group of


statements when the expression is true.

• The optional elseif and else keywords provide for the execution of alternate
groups of statements.

• An end keyword, which matches the if, terminates the last group of
statements.

20
IF-Statement

• The if statement evaluates a logical expression and executes a group of


statements when the expression is true.

• The optional elseif and else keywords provide for the execution of alternate
groups of statements.

• An end keyword, which matches the if, terminates the last group of
statements.

21

You might also like