Interpreting Matlab function simp

In summary, the conversation discusses the use of Simpson's rule for function integration, specifically looking at the Matlab command and its input arguments. The function file for simp() is referenced and its inputs are explained, including the use of different variables such as xl and xu. It is determined that simp can be used uniformly for every type of variable as long as their order is correct.
  • #1
Ein Krieger
34
0
Hello, guys

Hey guys,

Got stuck with function integration using Simpson's rule and need your help.

Please first refer to picture attached for full idea of my question:

The Matlab command related to it is:

for i=1:nr

u1d(i)=4.0*pi*r(i)^2*u(it,i)

end

I1=simp(0.0,r0,nr,u1d)/(4.0/3.0*pi*r0^3)

I1 is

nr=21

r0=1.0;

Does it mean that I1 is integrated 21 times between boundaries 0 and r0?
 

Attachments

  • 2.jpg
    2.jpg
    36.9 KB · Views: 481
Physics news on Phys.org
  • #2
Can you post more information? The code you posted references variables that you never define. Make it so that your code block can be copy/pasted into matlab.
 
  • #3
Yes. Sure

I have attached all commands with order from Pic.1 to Pic.3
 

Attachments

  • Pic 1.jpg
    Pic 1.jpg
    6.9 KB · Views: 420
  • Pic 2.jpg.png
    Pic 2.jpg.png
    22.4 KB · Views: 504
  • pic 3.jpg
    pic 3.jpg
    26.9 KB · Views: 436
  • #4
simp() is not a MATLAB function, so the information about the input arguments is not available in the documentation. I suggest looking at the function file for simp() to find this info.
 
  • #5
EDIT: I found information about this function in the MATLAB file exchange.

Code:
function s = simp(f, a, b, h)
x1 = a + 2 * h : 2 * h : b - 2 * h;
sum1 = sum(feval(f, x1));
x2 = a + h : 2 * h : b - h;
sum2 = sum(feval(f, x2));
s = h / 3 * (feval(f, a) + feval(f, b) + ...
           2 * sum1 + 4 * sum2);

It appears that the inputs are:

f=function, a=initial value, b=end value, h=interval size
 
Last edited:
  • #6
I have found it as separate m.file. Here are the commands:

function uint=simp(xl,xu,n,u)
h=(xu-xl)/(n-1);
uint(1)=u(1)-u(n);
for i=3:2:n
uint(1)=uint(1)+4.0*u(i-1)+2.0*u(i);
end
uint=h/3.0*uint;
But why here different variables are used such as xl and xu?

It seems to me that we use r in integration?
 
  • #7
At a glance it looks like

xl = beginning of interval
xu = end of interval
n = number of slices
u = function

So in your case of
Code:
I1=simp(0.0,r0,nr,u1d)

You are integrating u1d from 0 to r0 with nr intervals
 
  • #8
kreil said:
At a glance it looks like

xl = beginning of interval
xu = end of interval
n = number of slices
u = function

So in your case of
Code:
I1=simp(0.0,r0,nr,u1d)

You are integrating u1d from 0 to r0 with nr intervals

So you mean simp can be uniformly used for every type of variable assuming their correct order?
 

1. What is the purpose of the Matlab function simp?

The Matlab function simp is used for numerical integration, specifically for approximating the value of a definite integral. It uses the Simpson's rule to divide the interval of integration into smaller segments and then calculates the area under the curve using a quadratic polynomial.

2. How do I use the Matlab function simp?

To use the Matlab function simp, you need to provide the function you want to integrate as well as the lower and upper limits of the interval. You can also specify the number of segments to use for the approximation. The function will then return the estimated value of the integral.

3. What is the accuracy of the Matlab function simp?

The accuracy of the Matlab function simp depends on the number of segments used for the approximation. As the number of segments increases, the accuracy of the approximation also increases. However, the function may not provide a completely accurate value for certain functions with complex behavior.

4. Can the Matlab function simp be used for multidimensional integration?

No, the Matlab function simp is only designed for one-dimensional integration. For multidimensional integration, you can use other functions such as quad or integral2.

5. Are there any limitations to using the Matlab function simp?

The Matlab function simp may not be suitable for functions with high oscillations or sharp spikes, as it uses a quadratic polynomial for the approximation. It may also not be accurate for functions with infinite or undefined values within the interval of integration.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
17K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top