So for introduction,
I'm trying to create a program in my Ti Nspire CX CAS that will give me the solution (with working out) to approximating areas under a curve using the Simpsons Rule/Law.
For those unfamiliar with the "Simpsons Rule" or the like, it is simply a formula that approximates the answer of an integral.
For example: Integral of 2x with limits 0 to 3 would result in an exact answer of 3.
The Simpsons Rule uses the following equation so solve
h/3[(first ordinate + last ordinate) + 4(sum of odd ordinates) +2(sum of even ordinates)]
So My Code,
In order to understand why the code below is doing what it does (for those who do not understand the Simpsons Rule, or need to freshen up) I suggest you Google 'Simpsons Rule'. =P
Define LibPub srule(b,a,n)= Prgm :Local b,a,h,n,x,xx :((b-a)/(n))→h :a→x :2*x^(2)→a :Disp "Usage: srule(b,a,n)" :Disp "(where n=number of strips & b,a=integral limits)" :0→xx :Disp "x"&string(xx)&"="&string(a) :While x<b :DelVar a :xx+1→xx :x+h→x :2*x^(2)→a :Disp "x"&string(xx)&"="&string(a) :Cycle :EndWhile :Disp "End of Program" :EndPrgm
The Result of My Code (So Far),
Usage: srule(b,a,n) (where n=number of strips & b,a=integral limits) "x0=0" "x1=8" "x2=32" "x3=72" End of Program
"Sooo, Whats the Problem?
Well, the output of the code is correct, the working out for the (not yet found) solution is being displayed correctly. the problem is, if you look back at the code, you will see the function that we are trying to find the area of is hard-coded into the program: 2*x^(2) (Found twice in the code).
I am looking for a way to use the program by being able to state what the function is that we are trying to approximate the area of.
- I have tried using y as a variable: srule(y,b,a,n) - however when writing 2*x^(2) It is treated as a string (or something) and outputs the answer as 2*x^(2) INSTEAD OF actually substituting in the x value, that was very well defined in the program beforehand grr =(
So I guess my problem is, how do I do calculations from a variable that contains x values defined inside the program? Do I need to Define a Function of some sort? Or am I just making a silly mistake somewhere? Or is it all wrong =( ?