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

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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