double integral by a single variable by simpsons method

8 views (last 30 days)
Namira
Namira on 30 Sep 2016
Commented: elias GR on 3 Oct 2016
I want to calculate the double integral by a single variable by simpsons method. This is my code. I have a function f(x,y). I want to integrate it wrt x and then it integrate again wrt x. First I used the code for first integration. Then the result I put as a function f(x,y) and integrate again. I am confused about my code. Is it the right way to solve it? Or is there any simplest way to solve it?
function [s] = simprl(f,a,b,n) % f is the function to be integrated; a = initial value of the interval; b = final value of the interval; n = No. of subintervals
% The function implements the Simpson's Rule
h = (b-a)./n;
s1 = 0; % The variable s1 is initialised to 0.
s2=0; % The variable s2 is initialised to 0.
% loop for odd values in the range
for k = 1:n/2; % The index variable k starts at 1, then increases in steps of 1 until it reaches n/2.
x = a + h*(2*k-1);
s1 = s1+feval(f,x); % Each time through the loop the value of feval(f,x) is added to s1.
end
% loop for even values in the range
for k = 1:(n/2 - 1);
x = a + h*2*k;
s2 = s2+feval(f,x);
end
% Final result of integration where odd values are multiplied by 4 and even values are multiplied by 2
s = h*(feval(f,a)+feval(f,b)+4*s1+2*s2)/3;
  1 Comment
elias GR
elias GR on 3 Oct 2016
What do you mean by "double integral by a single variable by simpsons method". Can you give the analytic expression of the integral that you want to calculate? Furthermore, you say that "I want to integrate it wrt x and then it integrate again wrt x.". What wrt is? Why do you want to integrate twice?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!