// // TrapRule.sce // // Program to integrate a function using trap rule // // To run, use 'File' then 'Exec'. // // Michael Coburn // //Definition of the function function foo = f(x) foo = 5*x + sin(9*x - 5)/x endfunction //Set the time length and stop size steps = 100; x_start = 1; x_end = 10; delta_x = (x_end - x_start)/steps; total = 0; //A variable to hold the summation //Loop for trapezoid integration for i = 0:steps, x = x_start + i*delta_x; if i == 0 then total = total + f(x); elseif i == steps then total = total + f(x); else total = total + 2*f(x); end end total = total * delta_x/2; printf("Trapezoidal integration value %f\n", total);