HOw do I write a program to calculate definite integration? Please explain
HOw do I write a program to calculate definite integration? Please explain
What is the programming language you are using? If you are using python then I can help you.
You will need to import the scipy module for this and here is an example of this.
Code:>>> import scipy.integrate >>> func = lambda x: x**2 >>> scipy.integrate.quadrature(func, 0.0, 1.0) Took 4 points. (0.333333333333, 0.0)
Here is the code that can help you
Code:using System; namespace SimpsonMethod { public class sim { public static void Main() { int itr = 100000; double x, str, ed, dst, sm = 0, smtt = 0; Console.Write("Input range str: "); str = double.Parse(Console.ReadLine()); Console.Write("Input range ed: "); ed = double.Parse(Console.ReadLine()); dst = (ed - str)/itr; for (int i = 1; i <= itr; i++) { x = str + i * dst; smtt += f(x - dst / 2); if (i < itr) { sm += f(x); } } sm = (dst / 6) * (f(str) + f(ed) + 2 * sm + 4 * smtt); Console.WriteLine("Value on range [{0}, {1}] is around: {2:0.00000}", str, ed, sm); } private static double f(double x) { return x*x+2*x; } } }
I'm using C... Is there a way to write in in a for loop? I have no clue what I'm doing, an explaination of a program would also be very helpful. Thanks
This is the logic behind this code.
Here is the for loop
You have to just implement the same using c language.Code:for (int i = 1; i <= itr; i++) { x = str + i * dst; smtt += f(x - dst / 2); if (i < itr) { sm += f(x); } }
alrighty, i'll work on it. thanks
Bookmarks