<< Chapter < Page Chapter >> Page >

Evaluate f(x) at 5.

f(x) 3 x 2 2 x 1

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>>

The roots Function

Consider the following equation:

a x 2 b x c 0

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:

0.6 x 2 0.3 x 0.9 0

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>>

Splitting a statement

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

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

Basic operations

Basic operations.
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

Special characters

Special Characters
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

Summary of key points

  1. MATLAB has the common functions found on a scientific calculator and can be operated in a similar way,
  2. MATLAB can store values in variables. Variables are case sensitive and some variables are reserved by MATLAB (e.g. pi stores 3.1416),
  3. Variable Editor can be used to enter or manipulate matrices,
  4. The coefficients of simultaneous linear equations and polynomials are used to form a row vector. MATLAB then can be used to solve the equations,
  5. The format function is used to control the number of digits displayed,
  6. Three periods "..." at the end of the line is used to split a long statement over multiple lines,
  7. The percent symbol % separates the comments from the code, anything following % symbol is ignored by MATLAB.

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A brief introduction to engineering computation with matlab. OpenStax CNX. Nov 17, 2015 Download for free at http://legacy.cnx.org/content/col11371/1.11
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?

Ask