0

I am facing some problem during calculation of Numerical Integration with two data set. For integration i am using simpsons 1/3 rule.

    function I = Simpsons(f,a,b,n)
    if numel(f)>1 % If the input provided is a vector
        n=numel(f)-1; h=(b-a)/n;
        I= h/3*(f(1)+2*sum(f(3:2:end-2))+4*sum(f(2:2:end))+f(end));
    else 
        h=(b-a)/n; xi=a:h:b;
        I= h/3*(f(xi(1))+2*sum(f(xi(3:2:end-2)))+4*sum(f(xi(2:2:end)))+f(xi(end)));
    end

This code correctly calculates the integration.

Now the problem occurs during the calculation of multiplied values.

for example I have two functions f and g both are depending on same variable. the variables is in the same ranges. SO lower Limit and Upper Limit is same.

$\int_a^b \! f(x) *g(x) \, \mathrm{d} x.$

here the resolution of x is different. Means for f(x) we have 1000 data where as for g(x) we have 1700 data points. so element by element multiplication cant be done.

How to Solve this integration ..

salmannsu
  • 193
  • 1
  • 9

1 Answers1

1

you'll need to interpolate one of your functions, f or g, to the other function points, for 1D functions that is achievable using interp1.

For example:

% x1 an x2 have the same limits but different # of elements

 x1 = linspace(-10,10,100); 
 x2 = sort(rand(1,170)*20-10); # non-unifrom points from -10 to 10

 f1 = sin(x1);
 f2 = cos(x2);

now say we want to multiply f1*f2, we need them to have the # of elements, so

 f2i= interp1(x2,f2,x1,'spline');

will make f2 to have the same # of elements as f1, or instead

 f1i= interp1(x1,f1,x2,'spline');

will make f1 to have the same # of elements as f2.

bla
  • 25,846
  • 10
  • 70
  • 101
  • I was about to do interpolation. But if i do so then i need to pick individual points. Which will be a tedious job. Is there any simplest way ..(Here I am talking about Microsoft Excel Data Interpolation) – salmannsu Jan 13 '13 at 18:48
  • I don't understand what's tedious about it... since you have xi, what so hard in setting instead of xi some other vector `x=linspace(xi(1),xi(end),N)`? – bla Jan 13 '13 at 18:55
  • Sorry my comments. As i have all data in excel file i was trying to do the interpolation using excel. http://peltiertech.com/WordPress/excel-interpolation-formulas/ This link shows some graphical representations. I thought i need to take each value using cursors. Anyway Thanks for your Help. I am trying all in matlab now. – salmannsu Jan 13 '13 at 19:05
  • Now i see another problem. The way you have showed x1 and x2 calculation has uniform differences/resolution. In my data set the problem is separation between x for x2 is equal where as x1 is not (X1 has non uniform data where as X2 has uniform data). x1 is the practical data. so the people did the calculation has non uniform range. How to handle this one. – salmannsu Jan 13 '13 at 20:02
  • x1 and x2 are vectors of points, they need not be uniform in order to implement the `interp1`. See my edited answe, where I define x2 to be a random vector, it still works. – bla Jan 13 '13 at 21:37