Learning a new skill, especially a computer program in this case, can be overwhelming. However, if we build on what we already know, the process can be handled rather effectively. In the preceding chapter we learned about MATLAB Graphical User Interface (GUI) and how to get help. Knowing the GUI, we will use basic math skills in MATLAB to solve linear equations and find roots of polynomials in this chapter.
The evaluation of expressions is accomplished with arithmetic operators as we use them in scientific calculators. Note the addtional operators shown in the table below:
Operator | Name | Description |
---|---|---|
+ | Plus | Addition |
- | Minus | Subtraction |
* | Asterisk | Multiplication |
/ | Forward Slash | Division |
\ | Back Slash | Left Matrix Division |
^ | Caret | Power |
.* | Dot Asterisk | Array multiplication (element-wise) |
./ | Dot Slash | Right array divide (element-wise) |
.\ | Dot Back Slash | Left array divide (element-wise) |
.^ | Dot Caret | Array power (element-wise) |
The following figure illustrates a typical calculation in the Command Window.
MATLAB allows us to build mathematical expressions with any combination of arithmetic operators. The order of operations are set by precedence levels in which MATLAB evaluates an expression from left to right. The precedence rules for MATLAB operators are shown in the list below from the highest precedence level to the lowest.
MATLAB has all of the usual mathematical functions found on a scientific calculator including square root, logarithm, and sine.
pireturns the number 3.1416. To find the sine of pi, type in
sin(pi)and press enter.
sin(90*pi/180).
logreturns the natural logarithm of the value. To find the ln of 10, type in log(10) and press enter, (ans = 2.3026).
log10for common (base 10) logarithm. To find the log of 10, type in log10(10) and press enter, (ans = 1).
Practice the following examples to familiarize yourself with the common mathematical functions. Be sure to read the relevant help
and doc
pages for functions that are not self explanatory.
Calculate the following quantities:
MATLAB inputs and outputs are as follows:
2^3/(3^2-1)
(ans = 1)sqrt(5)-1
(ans = 1.2361)pi/4*2^2
(ans = 3.1416)Calculate the following exponential and logarithmic quantities:
MATLAB inputs and outputs are as follows:
exp(2)
(ans =
7.3891)log((5^10))
(ans =
16.0944)log10(10^5)
(ans =
5)Calculate the following trigonometric quantities:
MATLAB inputs and outputs are as follows:
cos(pi/6)
(ans =
0.8660)tan(45*pi/180)
(ans =
1.0000)sin(pi)+cos(45*pi/180)
(ans =
0.7071)formatFunction
The format
function is used to control how the numeric values are displayed in the Command Window. The short
format is set by default and the numerical results are displayed with 4 digits after the decimal point (see the examples above). The long
format produces 15 digits after the decimal point.
Calculate and display results in short
and long
formats.
The short
format is set by default:
>> theta=tan(pi/3) theta = 1.7321 >>
And the long
format is turned on by typing format long
:
>> theta=tan(pi/3) theta = 1.7321 >> format long >> theta theta = 1.732050807568877
In MATLAB, a named value is called a variable. MATLAB comes with several predefined variables. For example, the name pi refers to the mathematical quantity π, which is approximately pi ans = 3.1416
Variables in MATLAB are generally represented as matrix quantities. Scalars and vectors are special cases of matrices having size 1x1 (scalar), 1xn (row vector) or nx1 (column vector).
The term scalar as used in linear algebra refers to a real number. Assignment of scalars in MATLAB is easy, type in the variable name followed by = symbol and a number:
a = 1
Elements of a row vector are separated with blanks or commas.
Let's type the following at the command prompt:
b = [1 2 3 4 5]
We can also use the New Variable button to assign a row vector. In the tool strip, select Home > New Variable. This action will create a variable called unnamed which is displayed in the workspace. By clicking on the title unnamed, we can rename it to something more descriptive. By double-clicking on the variable, we can open the Variable Editor and type in the values into spreadsheet looking table.
Elements of a column vector is ended by a semicolon:
c = [1;2;3;4;5;]
Or by transposing a row vector with the ' operator:
c = [1 2 3 4 5]'
Or by using the Variable Editor:
Matrices are typed in rows first and separated by semicolons to create columns. Consider the examples below:
Let us type in a 2x5 matrix:
d = [2 4 6 8 10; 1 3 5 7 9]
This example is a 5x2 matrix:
Systems of linear equations are very important in engineering studies. In the course of solving a problem, we often reduce the problem to simultaneous equations from which the results are obtained. As you learned earlier, MATLAB stands for Matrix Laboratory and has features to handle matrices. Using the coefficients of simultaneous linear equations, a matrix can be formed to solve a set of simultaneous equations.
Let's solve the following simultaneous equations:
First, we will create a matrix for the left-hand side of the equation using the coefficients, namely 1 and 1 for the first and 2 and -5 for the second. The matrix looks like this:
The above matrix can be entered in the command window by typing A=[1 1; 2 -5]
.
Second, we create a column vector to represent the right-hand side of the equation as follows:
The above column vector can be entered in the command window by typing B= [1;9]
.
To solve the simultaneous equation, we will use the left division operator and issue the following command: C=A\B
. These three steps are illustrated below:
>> A=[1 1; 2 -5] A = 1 1 2 -5 >> B= [1;9] B = 1 9 >> C=A\B C = 2 -1 >>
The result C
indicating 2 and 1 are the values for x
and y
, respectively.
In the preceding section, we briefly learned about how to use MATLAB to solve linear equations. Equally important in engineering problem solving is the application of polynomials. Polynomials are functions that are built by simply adding together (or subtracting) some power functions. (see Wikipedia).
The coeffcients of a polynominal are entered as a row vector beginning with the highest power and including the ones that are equal to 0.
Create a row vector for the following function:
Notice that in this example we have 5 terms in the function and therefore the row vector will contain 5 elements. p=[2 3 5 1 10]
Create a row vector for the following function:
In this example, coefficients for the terms involving power of 3 and 1 are 0. The row vector still contains 5 elements as in the previous example but this time we will enter two zeros for the coefficients with power of 3 and 1: p=[3 0 4 0 -5]
.
polyvalFunction
We can evaluate a polynomial p
for a given value of x
using the syntax polyval(p,x)
where p contains the coefficients of polynomial and x is the given number.
Evaluate f(x) at 5.
The row vector representing f(x) above is p=[3 2 1]
. To evaluate f(x) at 5, we type in: polyval(p,5)
. The following shows the Command Window output:
>> p=[3 2 1] p = 3 2 1 >> polyval(p,5) ans = 86 >>
rootsFunction
Consider the following equation:
Probably you have solved this type of equations numerous times. In MATLAB, we can use the roots
function to find the roots very easily.
Find the roots for the following:
To find the roots, first we enter the coefficients of polynomial in to a row vector p with p=[0.6 0.3 -0.9]
and issue the r=roots(p)
command. The following shows the command window output:
>> p=[0.6 0.3 -0.9] p = 0.6000 0.3000 -0.9000 >> r=roots(p) r = -1.5000 1.0000 >>
You will soon find out that typing long statements in the Command Window or in the the Text Editor makes it very hard to read and maintain your code. To split a long statement over multiple lines simply enter three periods "..." at the end of the line and carry on with your statement on the next line.
The following command window output illustrates the use of three periods:
>> sin(pi)+cos(45*pi/180)-sin(pi/2)+cos(45*pi/180)+tan(pi/3) ans = 2.1463 >> sin(pi)+cos(45*pi/180)-sin(pi/2)... +cos(45*pi/180)+tan(pi/3) ans = 2.1463 >>
Comments are used to make scripts more "readable". The percent symbol % separates the comments from the code. Examine the following examples:
The long statements are split to make it easier to read. However, despite the use of descriptive variable names, it is hard to understand what this script does, see the following Command Window output:
t_water=80; t_outside=15; inner_dia=0.05; thickness=0.006; Lambda_steel=48; AlfaInside=2800; AlfaOutside=17; thickness_insulation=0.012; Lambda_insulation=0.03; r_i=inner_dia/2 r_o=r_i+thickness r_i_insulation=r_o r_o_insulation=r_i_insulation+thickness_insulation AreaInside=2*pi*r_i AreaOutside=2*pi*r_o AreaOutside_insulated=2*pi*r_o_insulation AreaM_pipe=(2*pi*(r_o-r_i))/log(r_o/r_i) AreaM_insulation=(2*pi*(r_o_insulation-r_i_insulation)) ... /log(r_o_insulation/r_i_insulation) TotalResistance=(1/(AlfaInside*AreaInside))+ ... (thickness/(Lambda_steel*AreaM_pipe))+(1/(AlfaOutside*AreaOutside)) TotalResistance_insulated=(1/(AlfaInside*AreaInside))+ ... (thickness/(Lambda_steel*AreaM_pipe))+(thickness_insulation ... /(Lambda_insulation*AreaM_insulation))+(1/(AlfaOutside*AreaOutside_insulated)) Q_dot=(t_water-t_outside)/(TotalResistance*1000) Q_dot_insulated=(t_water-t_outside)/(TotalResistance_insulated*1000) PercentageReducttion=((Q_dot-Q_dot_insulated)/Q_dot)*100
The following is an edited version of the above including numerous comments:
% Problem 16.06 % Problem Statement % Calculate the percentage reduction in heat loss when a layer of hair felt % is wrapped around the outside surface (see problem 16.05) format short % Input Values t_water=80; % Water temperature [C] t_outside=15; % Atmospheric temperature [C] inner_dia=0.05; % Inner diameter [m] thickness=0.006; % [m] Lambda_steel=48; % Thermal conductivity of steel [W/mK] AlfaInside=2800; % Heat transfer coefficient of inside [W/m2K] AlfaOutside=17; % Heat transfer coefficient of outside [W/m2K] % Neglect radiation % Additional layer thickness_insulation=0.012; % [m] Lambda_insulation=0.03; % Thermal conductivity of insulation [W/mK] % Output Values % Q_dot=(t_water-t_outside)/TotalResistance % TotalResistance=(1/(AlfaInside*AreaInside))+(thickness/(Lambda_steel*AreaM))+ ... (1/(AlfaOutside*AreaOutside) % Calculating the unknown terms r_i=inner_dia/2 % Inner radius of pipe [m] r_o=r_i+thickness % Outer radius of pipe [m] r_i_insulation=r_o % Inner radius of insulation [m] r_o_insulation=r_i_insulation+thickness_insulation % Outer radius of pipe [m] AreaInside=2*pi*r_i AreaOutside=2*pi*r_o AreaOutside_insulated=2*pi*r_o_insulation AreaM_pipe=(2*pi*(r_o-r_i))/log(r_o/r_i) % Logarithmic mean area for pipe AreaM_insulation=(2*pi*(r_o_insulation-r_i_insulation)) ... /log(r_o_insulation/r_i_insulation) % Logarithmic mean area for insulation TotalResistance=(1/(AlfaInside*AreaInside))+(thickness/ ... (Lambda_steel*AreaM_pipe))+(1/(AlfaOutside*AreaOutside)) TotalResistance_insulated=(1/(AlfaInside*AreaInside))+(thickness/ ... (Lambda_steel*AreaM_pipe))+(thickness_insulation/(Lambda_insulation*AreaM_insulation)) ... +(1/(AlfaOutside*AreaOutside_insulated)) Q_dot=(t_water-t_outside)/(TotalResistance*1000) % converting into kW Q_dot_insulated=(t_water-t_outside)/(TotalResistance_insulated*1000) % converting into kW PercentageReducttion=((Q_dot-Q_dot_insulated)/Q_dot)*100
Command | Meaning |
---|---|
sum | Sum of array elements |
prod | Product of array elements |
sqrt | Square root |
log10 | Common logarithm (base 10) |
log | Natural logarithm |
max | Maximum elements of array |
min | Minimum elements of array |
mean | Average or mean value of arrays |
std | Standard deviation |
Character | Meaning |
---|---|
= | Assignment |
( ) | Prioritize operations |
[ ] | Construct array |
: | Specify range of array elements |
, | Row element separator in an array |
; | Column element separator in an array |
... | Continue statement to next line |
. | Decimal point, or structure field separator |
% | Insert comment line into code |
pi
stores 3.1416),format
function is used to control the number of digits displayed,