<< Chapter < Page | Chapter >> Page > |
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>>
roots
FunctionConsider 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/2r_o=r_i+thickness
r_i_insulation=r_or_o_insulation=r_i_insulation+thickness_insulationAreaInside=2*pi*r_i
AreaOutside=2*pi*r_oAreaOutside_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 Valuest_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 termsr_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_oAreaOutside_insulated=2*pi*r_o_insulation
AreaM_pipe=(2*pi*(r_o-r_i))/log(r_o/r_i) % Logarithmic mean area for pipeAreaM_insulation=(2*pi*(r_o_insulation-r_i_insulation)) ...
/log(r_o_insulation/r_i_insulation) % Logarithmic mean area for insulationTotalResistance=(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 kWQ_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,Notification Switch
Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?