ENGG 1801 Engineering Computing - PowerPoint PPT Presentation

About This Presentation
Title:

ENGG 1801 Engineering Computing

Description:

Want to be able to make decisions' Example revisited. Can do using MATLAB as a calculator ... equation. Roots of ax2 bx c=0. Roots set by discriminant ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 13
Provided by: amic86
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: ENGG 1801 Engineering Computing


1
ENGG 1801Engineering Computing
  • MATLAB Lecture 2 Background for Week 4 Tutorial
  • Simple Programming in MATLAB

2
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

3
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

4
Example revisited
  • Can do using MATLAB as a calculator
  • gtgt x 110
  • gtgt term 1./sqrt(x)
  • gtgt y sum(term)
  • Far easier to write as an M-file

5
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

6
L2Demo Version 1
  • n input(Enter the upper limit )
  • x 1n Matlab is case sensitive
  • term sqrt(x)
  • y sum(term)
  • What happens if n lt 1 ?

7
L2Demo Version 2
  • n input(Enter the upper limit )
  • if n lt 1
  • disp (Your answer is meaningless!)
  • end
  • x 1n
  • term sqrt(x)
  • y sum(term)

Jump to here if TRUE
Jump to here if FALSE
8
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

9
Roots of ax2bxc0
  • Roots set by discriminant
  • ? lt 0 (no real roots)
  • ? 0 (one real root)
  • ? gt 0 (two real roots)
  • MATLAB needs to make decisions (based on ?)

10
One possible M-file
  • Read in values of a, b, c
  • Calculate ?
  • IF ? lt 0
  • Print message No real roots? Go END
  • ELSEIF ? 0
  • Print message One real root? Go END
  • ELSE
  • Print message Two real roots
  • END

11
My M-file

  • Demonstration of an m-file
  • Calculate the real roots of a quadratic
    equation

  • clear all clear all variables
  • clc clear screen
  • 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 b2 - 4ac Calculate discriminant
  • Calculate number (and value) of real roots
  • if delta lt 0
  • fprintf('\nEquation has no real roots\n\n')

Header
Initialisation
Calculate ?
Make decisions based on value of ?
12
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
Write a Comment
User Comments (0)
About PowerShow.com