Domanda

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 ..

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top