Problems with Two Variables Programming in MATLAB

Š Two Scalars „ „ „

ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

„

x y A A

= = = =

Š Two Vectors

3; 5; x * y 15

„ „ „ „

Š Scalar and Vector „ „ „ „

x y A A

= = = =

x = 1:5; y = 1:0.5:3; A = x * y Error

Š Two Vectors (by element)

1:5; 5; x * y 5 10 15 20 25

„ „ „ „

x y A A

= = = =

1:5; 1:0.5:3; x .* y 1 3 6 10 15

206_M5

Problems with Two Variables

Example

Š Results of element-by-element calculations

Š Plot voltage vs time for various RC time constants

x 1

4

v = e −t /τ V0

5

1

1.5 y

3

2.0

tau=0.5 tau=1.0 tau=2.0

0.8

3

0.7

6

2.5 3.0

Voltage vs Time for Various Time Constants

1 0.9

0.6 Voltage

1.0

2

2

10 ?

15

0.5 0.4 0.3

Š All combinations of x and y „ „

0.2

[X Y] = meshgrid(x,y) A = X .* Y

0.1 0

206_M5

„ „ „ „

1

1.5

2

2.5 Time

3

3.5

4

4.5

5

4

Input / Output Š User Defined Input

v = e −t /τ V0

„

time = 0:0.1:5; tau = [0.5 1.0 2.0]; [TIME TAU] = meshgrid(time,tau); V = exp(-TIME./TAU); plot(time,V)

Prompt user for input z

„

Display Function z z

5

disp(var) disp('Output string')

Number to String Function z

206_M5

var = input('Prompt string')

Š Output Options

„

ELEC 206

0.5

206_M5

Example

„

0

3

num2str(var)

206_M5

6

1

Input / Output

Input / Output

Š Formatted Output „

z z z z z „

Š Formatted Output

fprintf('format string', var, ...) %f %e %w.pf \n \t

„

fixed point exponential notation w=width, p=precision linefeed (endl) tab

z z z

2.0 3.0 ⎤ ⎡ 1.0 ⎢98.6 100.1 99.2⎥ ⎦ ⎣ z

The answer is

Patient Patient Patient

5.25 volts.

206_M5

„ „ „

Š File name must be same as function name „

„ „ „

„

help add3

Š Used like built-in MATLAB functions „ „

x=[1 2 3]; add3(x)

206_M5

10

Š Degrees / Radians Conversion Functions

function [dist vel accel]=motion(t) % Function to calculate distance, velocity and acceleration accel = 0.5 * t; vel = accel .* t; dist = vel .* t; function s=add3(x) % Function that adds 3 to array x a=x+3; s=a;

206_M5

ELEC 206

add3.m

Š Comments used by help function

Example

Š Local variables not visible outside function „

function s=add3(x) % Function that adds 3 to array x s=x+3;

9

Š Functions with multiple outputs

„

8

Š Functions written as M-files

Functions

„

98.6 100.1 99.2

Functions

% RC Time Constant Example with User Input clear, clc % Request input from user v0 = input('Input initial voltage: '); tau = input('Input 3 time constants as a vector: '); last = input('Input max time: '); incr = input('Input time increment: '); time = 0:incr:last; [TIME TAU] = meshgrid(time,tau); V = v0*exp(-TIME./TAU); plot(time,V) legend(num2str(tau(1)),num2str(tau(2)),... num2str(tau(3))) 206_M5

1 temp of 2 temp of 3 temp of

7

Example Revisited

„

fprintf('Patient %3.0f temp of %6.1f\n',hist)

fprintf('The answer is %5.2f volts.\n', v)

206_M5

„

patient = 1:3; temp = [98.6, 100.1, 99.2]; hist = [patient;temp];

Example z

„

Example

11

„

Problem Statement z

Create and test two functions: Š DR to change from degrees to radians Š RD to change from radians to degrees

„

Input/Output Description

Vector of degree values

Table of degrees to radians

Vector of radian values

Table of radians to degrees

206_M5

12

2

Example „

MATLAB Solution

Hand Example degrees = radians * 180/pi z radians = degrees * pi/180 z

„

Algorithm Development Define vector of degree values Call DR function to find radians z Output results in a table z Define vector of radian values z Call RD function to find degrees z Output results in a table z

Degrees Radians 0 0 30 0.524 60 1.047 90 1.571

„

Functions function output=DR(x) %This function changes degrees to radians output=x*pi/180;

z

206_M5

function output=RD(x) %This function changes radians to degrees output=x*180/pi;

13

206_M5

MATLAB Solution

MATLAB Solution

%Example 5.4 clear, clc %Define a vector of degree values degrees = 0:15:180; % Call the DR function, and use it to find radians radians = DR(degrees); %Create a table to use in the output degrees_radians =[degrees;radians]; %Generate an output table disp('A table of degrees to radians') disp('degrees radians') fprintf('%6.0f %8.3f\n',degrees_radians) %Put a blank line in output to separate tables disp(' ') 206_M5

%Define a vector of radian values radians = 0:pi/12:pi; %Call the RD function, and use it to find degrees degrees = RD(radians); %Generate an output table disp('A table of radians to degrees') disp('radians degrees') fprintf('%9.3f %10.3f \n',[radians;degrees])

15

206_M5

Control Structures

Š Relational Operators

Sequence of steps performed one after another

„

Š Selection „ „ „

„ „

Condition evaluated as true or false if true, one set of statements executed if false, another set of statements executed

„ „

Š Repetition „ „

„

„ „

ELEC 206

< >= == ~=

Š Logical Operators

less than less than or equal to greater than greater than or equal to equal to not equal

„ „ „

& | ~

and or not

Š True and False

Repeat (loop through) a set of steps As long as a condition is true

206_M5

16

Conditional Expressions

Š Sequence „

14

17

1 0

true false 206_M5

18

3

Selection Structures

Selection Structures

Š find Command „ „

Š if Statement

Š Example „ „ „

if condition statements else statements end

Š Example with 2D Matrix...

19

Example function g = grade(x) %This function requires a scalar input if(x>=90) g = 'A'; elseif(x>=80) g = 'B'; elseif(x>=70) g = 'C'; elseif(x>=60) g = 'D'; else g = 'F'; end 206_M5

function g = gradev(x) % This function works with vector input A = find(x>=90); B = find(x>=80 & x=70 & x=60 & x