Sunteți pe pagina 1din 6

Physics 15a PSI MATLAB Tutorial #3: Numerical Integration

Matlab cannot solve integrals symbolically (other programs like Mathematica can do this), but it can solve integrals numerically. To understand how Matlab does this, think back to the definition of an integral you learned in calculus: ( ) ( )

An integral can be approximated by a series of boxes drawn under the curve (see figure below); as the number of boxes increases, the approximation gets better and better. This is how Matlab can calculate integrals - it calculates the area of a large number of these boxes and sums them up.

Image credit: http://hyperphysics.phy-astr.gsu.edu/hbase/integ.html For the example shown in the figure, the integral ( ) would look like this in Matlab:

%First set the limits of integration and the step size: dx=0.5; %for N=4 x=[0:dx:2]; %this defines a vector of x values ranging from 0 to 2 in steps of 0.5. You could also use x=linspace[0:2:N], and define N=4. %Define the function: f=7*x-8.5*x.^2+3*x.^3; %Calculate the area under the curve: area=sum(f.*dx)

Enter the code as shown above and see if you get the same values as those shown in the figure for various values of dx.

Physics 15a PSI 3.1 Example: Calculating Mean and Variance

MATLAB Tutorial #3

In our discussion of statistics we talked about finding the mean and variance of a probability distribution. These can be calculated using Matlab. As above, we would first define the limits of integration. Because we realistically cannot sum from positive to negative infinity, we need to choose values that are large compared to the mean of the function we are studying. The table below shows some useful integrals in standard notion and in Matlab code: Standard Notation Integral Mean Variance Gaussian ( ) ( ) ( [ ( ) ( ) ) ( ( ) ) ] Matlab Code Area=sum(f.*dx) Meanx=sum(f.*x.*dx) Variance= sum((x-Meanx).^2*f.*dx) G=(1/(sigma*sqrt(2*pi)))*exp(((x-mu).^2)./(2*sigma^2));

Write a script that will calculate the mean of a Gaussian that has =4 and =2. In Matlab, your code should look something like this:
%First define the variables well use in our function: mu=4; sigma=2; dx=.1; %This is our step size. x=[-100:dx:100]; %Set the limits of integration. You dont need to go to infinity, just a very large number. %This is the Gaussian function we will integrate: G=(1/(sigma*sqrt(2*pi)))*exp(-((x-mu).^2)./(2*sigma^2)); %Now find mean value. The mean should be close to mu. meanx=sum(x.*G.*dx)

Change the step size (dx) and limits of integration. How does this affect your mean value?

Physics 15a PSI 3.2 Practice Problems (Solutions are on the next page.) 1. Exponential Decay

MATLAB Tutorial #3

The probability that a particle will live for t seconds before decaying is given by: ( ) , where is the lifetime of the particle. In the case of a neutron, the lifetime is known to be 881 seconds (about 15 minutes). Calculate the mean, variance, and standard deviation of this distribution. 2. IQ Scores The intelligence quotient (IQ) scale is designed such that the IQs of the total populace are distributed normally (i.e. according to a Gaussian) with a mean of 100 and a SD of 15. For the sake of this problem, suppose we use the term genius to refer to anybody with an IQ in the top 1% of the population. a) What is the minimum IQ required to be a genius? b) What is the mean IQ of all geniuses? c) What is the standard deviation of IQs among the genius population?

Physics 15a PSI 3.2 Practice Problem Solutions 1. Exponential Decay

MATLAB Tutorial #3

To find the mean lifetime, we need the expectation value of the exponential function. Note that we change the limits of integration so they make sense for the range of an exponential.
In Matlab, we solve this integral: clear all; close all; tau=881; %lifetime of a free neutron is 881 seconds dt=0.1; tmax=10*tau; t=[0:dt:tmax]; %set limits of integration P=(1/tau).*exp(-t./tau); %Calculate mean t value using a Riemann sum. You should get something reasonably close to tau, which is the mean lifetime of a sample. If you do not, check that your dt is small enough. meant=sum(t.*P).*dt

To find the standard deviation and variance of the lifetimes: ( ) ) ( )

In Matlab: %Now find Variance and SD for this distribution, using a sum in place of the integral: variance=sum((t-meant).^2.*P.*dt) SD=sqrt(variance) %You should find that the variance = 7.7264e+05 and SD = 878.9978.

Physics 15a PSI 2. IQ Scores

MATLAB Tutorial #3

a) We want to know the minimum IQ needed to be considered a genius, so we need to know the minimum IQ of the top 1% of the population. We know that the IQ scores are distributed as a Gaussian, so we can figure out the minimum IQ needed to make the area under the curve 1%: [ ( ) ]

Top 1% of the population

IQ In Matlab, your code should look something like this:


%First define the variables well use in our function: mu=100; sigma=15; dx=.1; %This is our step size. GeniusIQ=135; %Pick a number to start with, you can change it later. %Set the limits of integration. You dont need to go to infinity, just a very large number. Twice the genius IQ is big enough. x=[GeniusIQ:dx:2*GeniusIQ]; %This is the Gaussian function we will integrate. G=(1/(sigma*sqrt(2*pi)))*exp(-((x-mu).^2)./(2*sigma^2)); plot(x,G) %If you want to check that you entered the Gaussian correctly, you can plot it. %To integrate, we need the area under the curve. Vary GeniusIQ to find where the area=0.01 Area=sum(G.*dx) %When you run the program, you should find that a min IQ of 135 gives an area under the curve of 1%.

Physics 15a PSI

MATLAB Tutorial #3

b) To find the mean genius IQ, we want to find the expectation value of the Gaussian: ( )

Note that this is not normalized. The new curve (the tail of the original Gaussian) is equal to only 1 of the original, so we need to multiple by 100 to get the total area under the new curve to equal 1.
%Now find mean genius IQ: meanGeniusIQ=100*sum(x.*G.*dx) %You should find that the mean genius IQ is 140.

c) To find the standard deviation of the genius IQs, we want to find the variance: ( ) ) ( )

(
In Matlab: %Now find SD for this distribution: variance=100*sum((x-meanGeniusIQ).^2.*G.*dx) SD=sqrt(variance)

%You should find that the standard deviation in the genius IQ is 4.8.

S-ar putea să vă placă și