Trapezoidal Rule

//Trapezoidal Rule

deff('y=f(x)','y=x^4')
a=input("Enter Lower Limit: ")
b=input("Enter Upper Limit: ")
n=input("Enter number of sum intervals: ")
h=(b-a)/n
add1=0
add2=0
for i=0:n
    x=a+i*h
    y=f(x)
    disp([x y])
    if(i==0)|(i==n)
        add1=add1+y
    else
        add2=add2+y
    end
end
I=(h/2)*(add1+2*add2)
disp(I,"Integration by Trapezoidal Rule is:")

Output
-->exec('D:\Scilab prog by me\Trapezoidal Rule.sce', -1)
Enter Lower Limit: -3
Enter Upper Limit: 3
Enter number of sum intervals: 12

  - 3.    81.

  - 2.5    39.0625

  - 2.    16.

  - 1.5    5.0625

  - 1.    1.

  - 0.5    0.0625

    0.    0.

    0.5    0.0625

    1.    1.

    1.5    5.0625

    2.    16.

    2.5    39.0625

    3.    81.

 Integration by Trapezoidal Rule is:  

    101.6875

2 comments:

  1. hello, i would like to know what is the purpose of the add 1 and add2, also what is another way of writing that "if"

    ReplyDelete
  2. Hi,
    How would you integrate the following
    y=((8*sin(%pi*x/15))^2)./x
    with Lower limit 0 and upper limit 100.
    Thanks for any help.

    ReplyDelete