Professional Documents
Culture Documents
09-Feb-13
Outline
Algorithms Input Function Controlling Output
09-Feb-13
Algorithms
Before writing any computer program, it is useful to first outline the steps that will be necessary. An algorithm is the sequence of steps needed to solve a problem. In a modular approach to programming, the problem solution is broken down into separate steps, and then each step is further refined until the resulting steps are small enough to be manageable tasks. This is called the top-down design approach.
Algorithm Example
Calculating the area of a circle 1. Get the input: the radius 2. Calculate the result, the area 3. Display the output For most programs, the basic algorithm 1. Get the input(s) 2. Calculate the result(s) 3. Display the result(s)
09-Feb-13
Input Function
MATLAB allows you to prompt for input interactively This is through the input function:
input(prompt_string) input(prompt_string, options_string)
prompt_string tells MATLAB what text to display while waiting for a user to type something options_string lets you specify how MATLAB should interpret the input
Use s to indicate the input is a string
Input Examples
Try >> radius=input(Enter the radius:) >> user_name = input(What is your name? \n, s) >> user_age = input(What is your age? \n)
09-Feb-13
Controlling Output
MATLAB gives you a number of ways to control how output is displayed Changing how numbers are displayed does not change the precision of the underlying computations To change the default precision of echoed numbers in the Command Window, use the format command
format Options
format <option> short long short e short g long e long g bank hex rat Description 4 digits after decimal 14 digits after decimal 5 digits plus exponent 5 digits, with or without exponent 15 digits plus exponent dollars and cents 4-bit hexadecimal approximate ratio of small integers Example 3.1416 3.141592653589793 3.1416e+000 3.1416 3.141592653589793e+000 3.14 400921fb54442d18 355/113
Try >> x=pi; >> format short, x >> format long, x >> format bank, x
09-Feb-13
fprintf function
fprintf can be used to control how output is displayed fprintf(format,data) fprintf(format,data1,data2, ) format is a string specifying how to display the data data, datax are variables or values (either scalar or array) Example:
fprintf(pi:%d pi rounded: %1.0f\n,pi,pi)
fprintf function
The %d characters in the format string are called conversion characters Each conversion character is a data placeholder in the format string Each conversion character has a number of optional formatting parameters These parameters go between the % and the conversion character (d, f, i, etc.)
09-Feb-13
fprintf function
%-8.2f
Number insertion marker Conversion character Optional Flag indicating positioning inside the field Field width & decimal precision
fprintf function
%? %d %e %f %g %s Results display value in standard decimal notation display value in exponential form display value in fixed(floating)-point form display as %e or %f, whichever is shorter display a string of characters (not used with numbers)
To display a %, use %% in fprintfs format For complete list of options, use >> help fprintf Try >> x=3.1253; >> fprintf(x= %.0f, %10.5f, %-10.2f, %%\n,x,x,x); >> fprintf(x= %.0f, %10.5f, %-10.1f, \n %%,x,x,x);
09-Feb-13
Test it out!
Write a script to take the radius of a circle as an input from the user, calculate the area, then print the area. Sample Input/Output
This program calculates the area of a circle Enter the radius: 5 The area of a circle with radius 5 equal to
78.54
>>
clc; fprintf('This program calculates the area of a circle\n'); r=input('Enter the radius:'); a=pi*r^2; fprintf('The area of a circle with radius %1.0f equal to %10.2f\n',r,a);