Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How can I calculate area under the curve using Matlab?

Status
Not open for further replies.

tigerkonsole

Junior Member level 3
Junior Member level 3
Joined
Aug 22, 2005
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,479
:?:Can anyone tell me how can i calculate area under the curve using matlab?
 

example cubic béziercurve matlab

I got this from a friend.

I hope it helps.

Matlab Application - Curve fitting etc.
Curve Fitting
Let us do some curve fitting. We need a curve. We already have a Bezier curve that we just generated. We will use that curve for some of the exercises. Since we can create the Bezier curve when we want using the script we will work with the matrix XY. It is useful to be able to save the data to a file. We can do it the following way
>> help save

SAVE Save workspace variables to disk.
SAVE fname saves all workspace variables to the binary "MAT-file"
named fname.mat. The data may be retrieved with LOAD. Omitting the
filename causes SAVE to use the default filename "matlab.mat".

SAVE fname X saves only X.
SAVE fname X Y Z saves X, Y, and Z. The wildcard '*' can be used to
save only those variables that match a pattern.

SAVE fname X Y Z -ascii uses 8-digit ASCII form instead of binary.
SAVE fname X Y Z -ascii -double uses 16-digit ASCII form.
SAVE fname X Y Z -ascii -double -tabs delimits with tabs.
SAVE fname X Y Z -v4 saves a MAT-file that MATLAB 4 can LOAD.
SAVE fname X Y Z -append adds the variables to an existing MAT-file.

Use the functional form of SAVE, such as SAVE('fname','var1','var2'),
when the filename or variable names are stored in strings.

See also LOAD, DIARY, FWRITE, FPRINTF.

Since we only want to save XY (curve data) .

>> save C:\Emem899\Matlab_Course\XY1.dat XY -ascii -double

We are ready to curve fit. Let us create another script file that will read the data we saved . We are going to choose the file using a file dialog box. Assuming the file is saved in the directory indicated above

Press New in the toolbar in the editor
Type in the following code and save it with a .m etension
% This is a script file to explore
% Curve fitting
% interpolation
% derivative
% integration
%

clear % this will clear all the variables from the workspace
% use with caution

% the text1 is a string variable. The \n represents a new line in the display
text1 = ['The function which is being plotted must' ...
'contain atleast 2 columns of data ' ...
'\nIt must be an ascii file. Please select it' ...
'\nin the dialog box and hit return\n'];


fprintf(text1) % displays text1 on screen
clear text1 % do not need it any more
% using the uigetfile dialog box

[file, path] = uigetfile('C:\Emem899\Matlab_Course\*.dat','File of type Data',200,200);

% file is the name of the file and
% path is the path for the file
file % this displays the file name
path
cd(path) % change to the appropriate directory
% knowing the filename it should be possible to use the matlab load function


text1=[' use the load command from the work space' ...
' \n load filename '];
fprintf(text1)

load(file);

who % list the variables in the workspace
% should find XY1 there
XY1 % check the data

Execute the script so far and see if it works

Now that we have the data

%-------------------------------------------------------------------------
%For polynomial curve fitting we will use the polyfit function
% for evaluation we will use the polyval function
% feel free to type help polyfit and help polyval in the workspace
% window

coeff = polyfit(XY1:),1),XY1:),2),1) % a linear fit
% the polynomial coefficients are stored in coeff

plot(XY1:),1),polyval(coeff,XY1:),1)),'--')
% note that it should be easier to do the above
% in two steps - calculate the curve
% and plot it. Matlab can handle fancy coding
% with fancy coding

hold on % allows multiple plots on the same figure

plot(XY1:),1),XY1:),2),'r-')

grid % draws the grid

xlabel('x value')
ylabel('y value')
title(' curve fit example')
%-----------------------------------------------------------------------

Lets get a bit fanciful here

clf reset % clear figure

% define color/linestyle for plotting
colorstyle = ['y--' 'g.' 'ko:' 'c*-.'];

for i = 1:4
hf = figure; %returns a figure handle to a new figure
figure(hf) % sets the figure
coeff = polyfit(XY1:),1),XY1:),2),i);
plot(XY1:),1),polyval(coeff,XY1:),1)),colorstyle(i))
hold on
plot(XY1:),1),XY1:),2),'r-')
grid
xlabel('x value')
ylabel('y value')
intval = int2str(i); % convert integer to string
title([' curve fit example- order of fit 'intval])

pause(5) % pause 5 seconds
delete(hf) % deletes the figure with the handle
end



Interpolation
Interpolation is when you need to calculate values for the dependent variable at points in between those you have. Matlab has several interpolation routines. interp1 (for 1 D interpolation), interp2 ( for 2 D interpolation) , interp3 ( for 3D interpolation), and interpn (for n D interpolation). Since we are working with 2 D curves we will be doing 1 D interpolation.
% read in a vlue for x and interpolate its y value

xint = input('Type in the value of x - to be interpolated: ');
yint = interp1(XY1:),1), XY1:),2),xint)

% If you need to interpolate on y

clear xint yint

yint = input('Type the value for y - interpolation for x: ');

xint = interp1(XY1:),2),XY1:),1),yint)

Xint = input(' An array of values for interpolation: ')
% the array must be enclosed in square paranthesis
% and elements seperated by spaces

Yint = interp1(XY1:),1),XY1:),2),Xint)



Derivatives
Matlab includes Maple among its toolboxes. This provides Symbolic math capability. So it is possible to get an analytical derivative. Here we are interested in numerical derivatives.
There are two ways to calculate derivatives:


Using a polynomial fit and taking derivative of the polynomial
coeff = polyfit(XY1:),1),XY1:),2),3); % a cubic polynomial
coeffder = polyder(coeff);

xval = input('Value of x at which the derivative is required: ');

xder = polyval(coeffder,xval);

disp(['The derivative at ',num2str(xval), ' is ', num2str(xder)])


The value of the derivative can be obtained by setting up a finite difference computation - useful for finite element and finite difference methods

eps1 = 0.001; % example of first forward difference
xder1 = (interp1(XY1:),1),XY1:),2),xval+ ps1)-interp1(XY1:),1),XY1:),2),xval))/ eps1;
disp(['The derivative at ',num2str(xval), ' is ', num2str(xder1)])


Integration
This is a procedure by which you estimate the area under the curve or function. There are functions for this task. trapz, quad, and quad8.

area1 = trapz(XY1:),1),XY1:),2))

To use quad and quad8, the function must be defined as a function m-file
 

matlab polyfit 3d

Hi
There are many ways, I will tell you the easiest, I assume you have a vector f which has the numeric values of sampled function and dx is the sample interval. The area under this function is

sum(abs(f))*dx;

cheers
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top