Numerical integration in matlab

Click For Summary

Discussion Overview

The discussion revolves around calculating the arc length of a specific mathematical function using numerical integration in MATLAB. Participants explore different methods for integration, including the trapezoid rule and Simpson's rule, and address issues related to variable dimensions and coding challenges.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on whether it is possible to calculate arc length using MATLAB, specifically asking for coding suggestions.
  • Another participant clarifies that the integration is to be performed over the variable t, and expresses the need for arc lengths corresponding to different values of k and d.
  • A participant suggests using MATLAB's trapz function for numerical integration and mentions the quad function for function handles.
  • One participant shares their attempt to use the trapz function but encounters a matrix dimension error, indicating a misunderstanding of how to handle multiple variables simultaneously.
  • A brute force approach is proposed by another participant, detailing a nested loop structure to compute the arc length for various combinations of d and k, while noting the potential inefficiency of this method.

Areas of Agreement / Disagreement

Participants generally agree on the methods available for numerical integration but express differing opinions on the best approach to implement the calculations in MATLAB. There is no consensus on the optimal coding strategy or resolution of the encountered errors.

Contextual Notes

Participants mention issues related to matrix dimensions and the efficiency of brute force calculations, indicating that the implementation may require careful handling of variable sizes and computational resources.

avaquake
Messages
2
Reaction score
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
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
 
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).
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K