0

I need to write some code to numerically integrate the function:

y(x) = (e^-ax)cos(x)

I am required to implement my own function as oppose to importing from SciPy. How would I go about this?

Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102
  • Do you know how integrating works? – Willem Van Onsem Mar 10 '17 at 10:36
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – Łukasz Rogalski Mar 10 '17 at 10:37
  • Yes, i have a naive approximation for the integral of y(x) as being the sum from i=1 to N of y(xi)(xi - x(i-1) – timetabedlidalot Mar 10 '17 at 10:37
  • The practical answer is that the function you gave can be integrated analytically, which is much easier than doing it numerically in this case. Why are you required to do it numerically and without Scipy? Because it's homework? – Sven Marnach Mar 10 '17 at 10:44
  • While mathematically valid, that algorithm generally doesn't converge well, and suffers from rounding errors. Do you know Simpson's Rule? – PM 2Ring Mar 10 '17 at 10:45
  • It's what my assignment asks me to do – timetabedlidalot Mar 10 '17 at 10:46
  • The Wikipedia article on [Numerical Integration](https://en.wikipedia.org/wiki/Numerical_integration) is well worth reading. FWIW, [this answer](http://stackoverflow.com/a/33715116/4014959) contains an implementation of Simpson's Rule. – PM 2Ring Mar 10 '17 at 10:52
  • ok, maybe he needs a hint boys, have a look on this square function, do you understand it? def square(x): return x**2 – Vadim Mar 10 '17 at 10:54
  • I understand this, I just don't understand how to input the equation I defined for integration. It has to be this equation. – timetabedlidalot Mar 10 '17 at 11:01
  • @timetabedlidalot good, half way is done! so I suggest rephrase you question, give your code (or what you have done) and show what don't understand. Because now I can understand the question, sorry... – Vadim Mar 10 '17 at 11:06
  • OK, the bit I am struggling with is where I write return. I don't understand how to implement the approximation for integration that I have. This is the sum from i=1 to N of y(xi)(xi - x(i-1) – timetabedlidalot Mar 10 '17 at 11:23

0 Answers0