简体   繁体   中英

integrating ln(1+x) using trapezoidal rule in octave

I was trying to integrate ln(1+x) function from 1 to 2 using trapezoidal rule in octave.My program runs,gives output also but doesn't match with the accurate answer.Though spending plenty of time on it i couldn't find the bug. here is the function code

function [p] = integrationFunction ()
    n=10;
    y(n+1)=0;
    p(n+1)=0;
    a=1;b=2;h=(b-a)/n;
    for i=1:1:n+1
     y(i)=log(a+(i-1)*h);
     end
     p=y;
    endfunction

here is the main code.

p=integrationFunction();
b=2;
a=1;
n=10;
h=0.1;
trapi=0;
for i=1:1:n+1
  if(i==1 && i==(n+1))
  trapi=trapi+(h/2)*p(i);
  else
  trapi=trapi+h*p(i);
  end
  end
  trapi

sorry for any presentation inconvenience , new joiner of stack and in hurry also.accurate answer should be 0.90954 and mine shows 0.42054 the TRAPEZOIDAL RULE :h/2[(Y0+Yn)+2(Y1+Y2+Y3+...+Yn-1)]

As for the 'wrong result', I suspect it's mainly that the function you're calculating is the wrong function (as well as the stuff Andy has already pointed out). You seem to be estimating the integral of log(x) rather than log(1 + x) in your code.

Having said that, there are several 'programming logic' problems above, but the main one is that you use your function in a redundant way. It would make more sense to define a function that takes the function handle to process, n , a , and b as arguments, and returns the result. Then your main code would simply look like this:

trapi = integrationFunction ( @(x) log(1 + x), 10, 1, 2 );

Furthermore, as others have pointed out, this kind of operation is best done in a vectorised manner in languages like octave. Obviously, if you're at the stage where you're trying to get to grips with programming and for-loops in general, this could be forgiven at this point, but it's worth keeping in mind and learning appropriate practices alongside programming basics from the beginning.

Lastly, while I sympathise a bit with the "in a hurry to learn stackoverflow formatting just to ask a question" (though I also agree with Andy's criticism on this), I would argue that proper formatting of code (and at the very least proper indentation!) is not just about presentation on online forums, but helps you the developer to be able to read and maintain your code, and spot mistakes easily. I suspect that had the code been more legible, the fact you were calculating the wrong function would have stood out immediately.

Hi the main point of a function is that it can be reused with arguments. In its current state, integrationFunction is hard coded which defeats the purpose of a function.

To address the integral at hand, we solve it in a vectorised manner:

a = 1;
b = 2;
dx = (2-1) / 10000;     % set the size of each rectangle experiment with different values
f_vec = 1:dx:2;         % create a vector from 1 to 2 with intervals of dx
f_vec(1) = f_vec(1)*2;
sum(dx * log(f_vec + 1)) % apply log(x + 1) to the array, multiply by dx, sum the array.

See Trapezoidal rule for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM