<< Chapter < Page Chapter >> Page >

The trapezoidal rule

Sometimes it is rather convenient to use a numerical approach to solve a definite integral. The trapezoid rule allows us to approximate a definite integral using trapezoids.

The trapz Command

Z = trapz(Y) computes an approximation of the integral of Y using the trapezoidal method.

Now, let us see a typical problem.

Given Area x 2 5 x 2 , an analytical solution would produce 39. Use trapz command and solve it

  1. Initialize variable x as a row vector, from 2 with increments of 0.1 to 5: x=2:.1:5;
  2. Declare variable y as y=x^2; . Note the following error prompt: ??? Error using ==>mpower Inputs must be a scalar and a square matrix. This is because x is a vector quantity and MATLAB is expecting a scalar input for y. Because of that, we need to compute y as a vector and to do that we will use the dot operator as follows: y=x.^2; . This tells MATLAB to create vector y by taking each x value and raising its power to 2.
  3. Now we can issue the following command to calculate the first area, the output will be as follows:
area1=trapz(x,y) area1 =39.0050

Notice that this numerical value is slightly off. So let us increase the number of increments and calculate the area again:

x=2:.01:5; y=x.^2;area2=trapz(x,y) area2 =39.0001

Yet another increase in the number of increments:

x=2:.001:5; y=x.^2;area3=trapz(x,y) area3 =39.0000

Determine the value of the following integral:

x 0 x

  1. Initialize variable x as a row vector, from 0 with increments of pi/100 to pi: x=0:pi/100:pi;
  2. Declare variable y as y=sin(x);
  3. Issue the following command to calculate the first area, the output will be as follows:
area1=trapz(x,y) area1 =1.9998

let us increase the increments as above:

x=0:pi/1000:pi; y=sin(x);area2=trapz(x,y) area2 =2.0000

A gas expands according to the law, PV 1.4 =c. Initially, the pressure is 100 kPa when the volume is 1 m 3 . Write a script to compute the work done by the gas in expanding to three times its original volume O. N. Mathematics: 2 by J. Dobinson, Penguin Library of Technology. © 1969, (p. 184) .

Recall that PV diagrams can be used to estimate the net work performed by a thermodynamic cycle, see Wikipedia or we can use definite integral to compute the work done (WD) as follows:

WD v p

If we rearrange the expression pressure as a function of volume, we get:

P c V 1.4

By considering the initial state, we can determine the value of c:

c 100 1 1.4 100

From the equation and the equation above, we can write:

P 100 V 1.4

By inserting P in WD , we get:

WD v 1 3 100 v 1.4

For MATLAB solution, we will consider P as a function of V and WD . Now, let us apply the three-step approach we have used earlier:

  1. Initialize variable volume as a row vector, from 1 with increments of 0.001 to 3: v=1:0.001:3;
  2. Declare variable pressure as p=100./v.^1.4;
  3. Use the trapz function to calculate the work done, the output will be as follows:
WorkDone=trapz(v,p) WorkDone =88.9015

These steps can be combined in an m-file as follows:

clc disp('A gas expands according to the law, pv^1.4=C')disp('Initial pressure is 100 kPa when the volume is 1 m3') disp('Compute the work done by the gas in expanding')disp('To three times its original volume') disp(' ') % Display blank linev=1:.001:3; % Creating a row vector for volume, v p=100./(v.^1.4); % Computing pressure for volumeWorkDone=trapz(v,p) % Integrating p*dv over 1 to 3 cubic meters

A body moves from rest under the action of a direct force given by F 15 x 3 where x is the distance in meters from the starting point. Write a script to compute the total work done in moving a distance 10 m. O. N. Mathematics: 2 by J. Dobinson, Penguin Library of Technology. © 1969, (p. 183)

Recall that the general definition of mechanical work is given by the following integral, see Wikipedia :

WD x F

Therefore we can write:

WD x 0 10 15 x 3

Applying the steps we followed in the previous examples, we write:

clc disp('A body moves from rest under the action of a direct force given')disp('by F=15/(x+3) where x is the distance in meters') disp('From the starting point.')disp('Compute the total work done in moving a distance 10 m.') disp(' ') % Display blank linex=0:.001:10; % Creating a row vector for distance, x F=15./(x+3); % Computing Force for xWorkDone=trapz(x,F) % Integrating F*dx over 0 to 10 meters.

The output of the above code is:

A body moves from rest under the action of a direct force given by F=15/(x+3) where x is the distance in metersFrom the starting point. Compute the total work done in moving a distance 10 m.WorkDone =21.9951

Summary of key points

  1. In its simplest form, numerical integration involves calculating the areas of segments that make up the area under a curve,
  2. MATLAB has built-in functions to perform numerical integration,
  3. Z = trapz(Y) computes an approximation of the integral of Y using the trapezoidal method.

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