0% found this document useful (0 votes)
47 views

Engg1801 2005 Lect2

This document provides an outline for a MATLAB lecture on simple programming and decision making. It discusses using MATLAB as a calculator versus programming with M-files. M-files allow storing and running instructions sequentially while accepting inputs and outputs. Decision making in MATLAB is demonstrated using IF/ELSEIF/ELSE commands to determine the number and values of real roots of a quadratic equation based on the discriminant's value. An example M-file is provided to calculate roots.

Uploaded by

Glan Devadhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Engg1801 2005 Lect2

This document provides an outline for a MATLAB lecture on simple programming and decision making. It discusses using MATLAB as a calculator versus programming with M-files. M-files allow storing and running instructions sequentially while accepting inputs and outputs. Decision making in MATLAB is demonstrated using IF/ELSEIF/ELSE commands to determine the number and values of real roots of a quadratic equation based on the discriminant's value. An example M-file is provided to calculate roots.

Uploaded by

Glan Devadhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

ENGG 1801

Engineering Computing

MATLAB Lecture 2: Background for Week


4 Tutorial

Simple Programming in MATLAB


Outline of lecture
 MATLAB as a calculator revisited
 Concept of M-files
 Decision making in MATLAB
 Use of IF and ELSEIF commands
 Example: Real roots of a quadratic
MATLAB as a calculator
 MATLAB can be used as a ‘clever’ calculator
– This has very limited value in engineering

 Real value of MATLAB is in programming


– Want to store a set of instructions
– Want to run these instructions sequentially
– Want the ability to input data and output results
– Want to be able to plot results
– Want to be able to ‘make decisions’
Example revisited
n
1 1 1 1
y     ...
i 1 i 1 2 3
 Can do using MATLAB as a calculator
– >> x = 1:10;
– >> term = 1./sqrt(x);
– >> y = sum(term);
 Far easier to write as an M-file
How to write an M-file
 File → New → M-file
 Takes you into the file editor
 Enter lines of code (nothing happens)
 Save file (we will call ours L2Demo.m)
 Exit file
 Run file
 Edit (ie modify) file if necessary
L2Demo Version 1

 n = input(‘Enter the upper limit: ‘);


 x = 1:n; % Matlab is case sensitive
 term = sqrt(x);
 y = sum(term)

 What happens if n < 1 ?


L2Demo Version 2
 n = input(‘Enter the upper limit: ‘);
 if n < 1
disp (‘Your answer is meaningless!’)
 end
Jump to here if TRUE
 x = 1:n;
 term = sqrt(x); Jump to here if FALSE
 y = sum(term)
Decision making in MATLAB
 For ‘simple’ decisions?
 IF … END (as in last example)
 More complex decisions?
 IF … ELSEIF … ELSE ... END

 Example: Real roots of a quadratic equation


Roots of ax2+bx+c=0

 b  b 2  4ac
 Roots set by discriminant x
2a
 Δ < 0 (no real roots)
 Δ = 0 (one real root)
 Δ > 0 (two real roots)   b  4ac
2

 MATLAB needs to make


decisions (based on Δ)
One possible M-file
 Read in values of a, b, c
 Calculate Δ
 IF Δ < 0
 Print message ‘ No real roots’→ Go END
 ELSEIF Δ = 0
 Print message ‘One real root’→ Go END
 ELSE
 Print message ‘Two real roots’
 END
My M-file
 %==========================================================
 % Demonstration of an m-file
 % Calculate the real roots of a quadratic equation
 %==========================================================



clear all; % clear all variables
clc; % clear screen
Header
 coeffts = input('Enter values for a,b,c (as a vector): '); % Read in equation coefficients
 a = coeffts(1);
 b = coeffts(2);
 c = coeffts(3);

 delta = b^2 - 4*a*c; % Calculate discriminant


Initialisation
 % Calculate number (and value) of real roots

 if delta < 0
Calculate Δ
 fprintf('\nEquation has no real roots:\n\n')
 disp(['discriminant = ', num2str(delta)])
 elseif delta == 0
 fprintf('\nEquation has one real root:\n')


xone = -b/(2*a)
else
Make decisions
fprintf('\nEquation has two real roots:\n')
based on value of Δ

 x(1) = (-b + sqrt(delta))/(2*a);
 x(2) = (-b – sqrt(delta))/(2*a);
 fprintf('\n First root = %10.2e\n\t Second root = %10.2f', x(1),x(2))
 end
Conclusions
 MATLAB is more than a calculator
– its a powerful programming environment
 Have reviewed:
– Concept of an M-file
– Decision making in MATLAB
– IF … END and IF … ELSEIF … ELSE … END
– Example of real roots for quadratic equation
– Same structure as Week 4 tutorial problem

You might also like