Numerical integration in matlab

In summary, this code will calculate the arc length of a curve given its x, y, and d coordinates. It uses the trapezoid rule or Simpson's rule to do so, and can be moderately fast depending on the complexity of the curve. It can overestimate the area if the gradient is negative, and underestimate the area if the gradient is positive.
  • #1
avaquake
2
0
i am very new in matlab. and i need to calculate the arc lengh. My equation is
arc length = integration of sqrt(d.*(k.^2-2).*sin(k.*t./2).^2 - d.*(k.^2-1).*sin(k.*t./2).^4 + 1); from 0 to 2.pi
where, d=(0:.1:1) and k=(0:1:10)

can anybody tell me whether it is possible to calculate this arc length using matlab. If possible can you suggest me the coding to write the script?

thanks.
 
Physics news on Phys.org
  • #2
avaquake said:
i am very new in matlab. and i need to calculate the arc lengh. My equation is
arc length = integration of sqrt(d.*(k.^2-2).*sin(k.*t./2).^2 - d.*(k.^2-1).*sin(k.*t./2).^4 + 1); from 0 to 2.pi
where, d=(0:.1:1) and k=(0:1:10)

can anybody tell me whether it is possible to calculate this arc length using matlab. If possible can you suggest me the coding to write the script?

thanks.

Welcome to PhysicsForums!

First off, are you integrating over k, or over t? Do you just want arclengths for different values of k?

Secondly, there are two ways of doing this: trapezoid rule, or with Simpson's rule.

Given a vector of x, and an array of y, you can use MATLAB's trapz function:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html

With a function handle (see inside the documentation page for a link--think of it as an inline function), you can use the MATLAB quad function:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/quad.html
 
  • #3
i am integrating with repest to t. and yes i want arclength for different values of k as well as d.
for example: when d=.1, k=1:1:10, this will give me 10 different value of arclength.
when d=.2, k=1:1:10, this will again give me another 10 differenct value of arclength.
......
......
until d=1.

i have tried with the following to apply Z = trapz(X,Y).
>> x= 0:2*pi/100:2*pi;
>> k=(1:.1:10);
>> y=sqrt(1+0.1.*(k.^2-2).*sin(k.*x./2).^2-0.1.*(k.^2-1).*sin(k.*x./2).^4);
but this line give me an error msg.
? Error using ==> times
Matrix dimensions must agree.

another problem is that i have fixed the value of d=.1 , as i do not know how to set the value of two variables at a time so that it gives me the results of arclength as my requirement(as i give an example in the beginning).
 
  • #4
Brute force

Code:
>> d = 0:0.1:1;
>> k = 0:1:10;
>> t = 0:0.01:2*pi;
>> size(t)
ans =
     1   629


>> for i = 1:11
>> for j = 1:11
>> for l = 1:629
>> f(i,j,l) = sqrt(d(i)*(k(j)^2-2)*sin(k(j)*t(l)/2)^2 - d(i)*(k(j)^2-1)*sin(k(j)*t(l)/2)^4 + 1);
>> 100*i*j*l/(629*11*11)
>> end
>> end
>> end
>> area = sum(f*0.01,3);

For each parameter configuration (there are 121 in your post = 11*11) this calculates the height of the curve you gave, from 0 to 2*pi in intervals of 0.01 (629 total calculations).

It then approximates the curve as a step function, or 629 different rectangles, and will therefore overestimate the area if the gradient is negative and underestimate the area if it is positive. It's a decent and simple first approximation.

Word of warning, this takes a long time.

area is an 11x11 double of your areas in parameter space. To see how it varies graphically,
Code:
>> surf(d,k,area)
Should work.
 
  • #5


Hello,

Yes, it is possible to calculate the arc length using Matlab. Matlab has built-in functions for numerical integration, such as "trapz" and "quad". These functions can be used to numerically integrate a given function over a specified range.

To calculate the arc length using the equation provided, you can follow these steps:

1. Define the variables d and k using the given ranges:
d = (0:.1:1);
k = (0:1:10);

2. Define the function to be integrated:
f = @(t) sqrt(d.*(k.^2-2).*sin(k.*t./2).^2 - d.*(k.^2-1).*sin(k.*t./2).^4 + 1);

3. Use the "quad" function to numerically integrate the function over the range from 0 to 2*pi:
arc_length = quad(f, 0, 2*pi);

4. The value of "arc_length" will give you the numerical value of the arc length.

You can also use a loop to calculate the arc length for different values of d and k. Here is an example code:

% Define the ranges for d and k
d = (0:.1:1);
k = (0:1:10);

% Pre-allocate an array to store the arc lengths for different values of d and k
arc_length = zeros(length(d), length(k));

% Loop through the values of d and k
for i = 1:length(d)
for j = 1:length(k)
% Define the function to be integrated
f = @(t) sqrt(d(i).*(k(j).^2-2).*sin(k(j).*t./2).^2 - d(i).*(k(j).^2-1).*sin(k(j).*t./2).^4 + 1);

% Use the "quad" function to numerically integrate the function over the range from 0 to 2*pi
arc_length(i,j) = quad(f, 0, 2*pi);
end
end

% The array "arc_length" will contain the calculated arc lengths for different values of d and k.

I hope this helps. Let me know if you have any further questions. Good luck with your calculations!
 

Related to Numerical integration in matlab

What is numerical integration in MATLAB?

Numerical integration in MATLAB is a method used to approximate the definite integral of a function by dividing the area under the curve into smaller, simpler shapes and summing their areas. It is a numerical approach as opposed to analytical integration, which involves finding the exact solution using mathematical formulas.

How do I perform numerical integration in MATLAB?

To perform numerical integration in MATLAB, you can use the built-in function "integral" or "quad" depending on the type of integration you need (single or multiple). These functions take in the function to be integrated, the limits of the integral, and other optional parameters to calculate an approximate solution.

What is the difference between "integral" and "quad" in MATLAB?

The "integral" function in MATLAB is used for single integration, where the limits of integration are fixed. On the other hand, the "quad" function is used for multiple integration, where the limits can vary for each integration. Additionally, "quad" is more accurate and efficient for complicated integrands.

Can I customize the accuracy of numerical integration in MATLAB?

Yes, you can use the optional parameters available in the "integral" and "quad" functions to customize the accuracy of numerical integration in MATLAB. For example, you can specify the desired tolerance or the maximum number of function evaluations to be used in the calculation.

What are some common applications of numerical integration in MATLAB?

Numerical integration in MATLAB is commonly used in various scientific and engineering fields, such as physics, chemistry, and economics. It is used to solve problems that involve finding areas, volumes, and other quantities that can be represented as integrals. Additionally, it is used in data analysis to approximate the integral of a given dataset.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
678
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
3K
Back
Top