Integration

Reply

Join Date: Jul 2009
Posts: 24
Reputation: AnnA.B is an unknown quantity at this point 
Solved Threads: 0
AnnA.B's Avatar
AnnA.B AnnA.B is offline Offline
Newbie Poster

Integration

 
0
  #1
Jul 18th, 2009
Hi!
I have a problem, again.
I would like to write a program to integrate functions. If I write to console integrate(polynom, lower limit, upper limit). I already write function which seperate polynom, now I don't know how to calculate whole thing.
Example: integrate(x^2+3,-1,2) solution: 12
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Integration

 
0
  #2
Jul 18th, 2009
I didn't understand everything...
You said,the function made by you...
Can you send some code?Too less information about your function....
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 24
Reputation: AnnA.B is an unknown quantity at this point 
Solved Threads: 0
AnnA.B's Avatar
AnnA.B AnnA.B is offline Offline
Newbie Poster

Re: Integration

 
0
  #3
Jul 19th, 2009
There are two functions, one is for 'reading' input, and second one is for calculation. Here is code:

  1. function RightStr(const AStr: String; Len: Integer): String;
  2. begin
  3. end;
  4.  
  5. function LeftStr(const AText: string; const ACount: Integer): string;
  6. begin
  7. end;
  8.  
  9.  
  10. function izraz (s1: string): string;
  11. var s2 : string;
  12. t,s: array[1..30] of char;
  13. j: integer;
  14. factor,potency,posx,pos_pot: integer;
  15. polinom: array[0..10] of integer;
  16.  
  17. begin
  18. repeat
  19. posx := pos('x',s2); //find x
  20.  
  21. if posx > 1 then // if we find x, then read characters before x
  22. begin
  23. j:=1;
  24. repeat
  25. t[j] := s2[j];
  26. j:=j+1;
  27. until ( s2[j] = 'x' ) ;
  28. factor := strtoint(t);
  29. end
  30. else // if x is on the first place, then factor is 1
  31. begin
  32. if posx = 1 then
  33. factor := 1
  34. else
  35. begin
  36. // t := niz1;
  37. s2 := '';
  38. factor := strtoint(t);
  39. end;
  40. end;
  41.  
  42. if posx >= 1 then // if x is found, search ^
  43. begin
  44. pos_pot := pos('^',s2);
  45. if pos_pot > 1 then
  46. begin
  47. s2 := rightstr(s2,length(s2)-pos_pot);
  48. j:=1;
  49. repeat
  50. s[j] := s2[j];
  51. j:=j+1;
  52. until ( s2[j] = '+' ) or ( s2[j] = '-' ) or ( j>length(s2) ) ;
  53. potency := strtoint(s);
  54. s2 := rightstr(s2,length(s2)-j+1);
  55. end
  56. else
  57. begin
  58. potency := 1;
  59. s2 := rightstr(s2,length(s2)-posx);
  60. end;
  61. end
  62. else
  63. potency := 0;
  64. polinom[potency] := factor;
  65. until s2 = '';
  66. end;
  67.  
  68.  
  69. function integral(input: string): extended;
  70. var p,i, t2 : integer;
  71. niz1: string;
  72. l: integer;
  73. lower_limit, upper_limit: string[2];
  74.  
  75. begin
  76. delete(input,1,9);
  77. delete(input,length(input),1);
  78. p:=pos(',',input);
  79. niz1:=copy(input,1,p-1);
  80. izraz(niz1);
  81. l:=length(input);
  82. t2:=l-2;
  83. begin
  84. if input[l-3]='-' then
  85. upper_limit:=copy(input,length(input[i-3]),2);
  86. if input[i-6]='-' then
  87. lower_limit:=copy(input,length(input[i-6]),2)
  88. else
  89. lower_limit:=inttostr(l-5);
  90.  
  91. if input[l-3]<>'-' then
  92. upper_limit:=inttostr(l-2);
  93. if input[l-5]='-' then
  94. lower_limit:=copy(input,length(input[i-5]),2)
  95. else
  96. lower_limit:=inttostr(l-4);
  97. end;
  98. end;
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Integration

 
0
  #4
Jul 19th, 2009
I want to understand but... my efforts wasn't success
  1. function integral(input: string): extended;//WAITS STRING ,BACK EXTENDED
  2. var p,//STORES THE ',' POSITION IN THE *INPUT* STRING
  3. i, t2 : integer;
  4. niz1: string;
  5. l: integer;
  6. lower_limit, upper_limit: string[2];
  7.  
  8. begin
  9. {
  10. procedure Delete(var S: String; Index: Integer; Count:Integer);
  11. Delete deletes Count characters from S starting at the Indexth position. If Index is larger than the length of S, no characters are deleted. If Count specifies more characters than remain starting at the Indexth position, the remainder of the string is deleted.
  12.   }
  13. delete(input,1,9);//DELETE(INPUT,from 1ST,9 CHARS)
  14. delete(input,length(input),1);//DELETE(INPUT,length of input,1 char)
  15. {AFTER THE TWO DELETE PROCEDURE THE *INPUT*'S VALUE IS EMPTY!!!!!}
  16. p:=pos(',',input);//HOW CAN IT FIND IN A EMPTY STRING????
  17. {
  18. SO WHEN I TRY TO USE YOUR FUNCTION THIS IS NOT WORKING AT ALL,AND WHERE IS THE BACK VALUE
  19. Integral:=?????? this is important part of a function....
  20. What do you want? Integrate a function to another?
  21. We need 'back value' to integrate them...
  22.  
  23. }
create a console apps and write your function there
like

Write(Integral('yourstring'));
ReadLn;{to stop the window to see the results}
press F7 to trace into the program step by step to see every commands one by one....
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 24
Reputation: AnnA.B is an unknown quantity at this point 
Solved Threads: 0
AnnA.B's Avatar
AnnA.B AnnA.B is offline Offline
Newbie Poster

Re: Integration

 
0
  #5
Jul 19th, 2009
the program should know how to solve definite integral(just like we do in math). Here is link to some theory about it: http://myhandbook.info/form_integ.html
http://aleph0.clarku.edu/~djoyce/ma121/rules.pdf
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Integration

 
0
  #6
Jul 20th, 2009
Kid if you're not already doing so, then use the trapezium rule.

Your program should take four arguments.

1) The function f(x)
2) The lower limit
3) The upper limit
4) The number of trapeziums to split it into
5) The height of the trapeziums

- Unless you're intend on creating somekinda generic polynomial class for trivial functions.
Last edited by iamthwee; Jul 20th, 2009 at 6:44 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 24
Reputation: AnnA.B is an unknown quantity at this point 
Solved Threads: 0
AnnA.B's Avatar
AnnA.B AnnA.B is offline Offline
Newbie Poster

Re: Integration

 
0
  #7
Jul 20th, 2009
iamthwee, I have been doing with integrals a lot, I still am (I'm a mathematician) I know all the rules of integration, but I don't know how to write a program which will solve integrals. As you see in my code, I split polynom and copy it into array where all numbers from polynom are saved. I also save lower and upper limit. Now, despite knowing rules for integration, I don't know how to calculate the whole thing in Delphi.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: squeege321 is an unknown quantity at this point 
Solved Threads: 0
squeege321 squeege321 is offline Offline
Newbie Poster

Re: Integration

 
0
  #8
Jul 20th, 2009
Hi Anna,

I'm pretty sure that I can help you with this, but of course I need to make sure I understand how to arrive at the correct answer to be of any help at all.

It has been about 4 years since the last time I was exposed to integrals. I am trying to solve your equation in order to understand what needs to be done programmatically. I am not finding your solution of 12. I'll walk you through my calculations. Show me where I'm wrong, and once I understand I can help.

So, we have
integrate(x^2+3,-1,2) solution: 12
the antiderivative of x^2+3 is (x^3)/3 + 3x
Solve for 2 = 2 2/3 + 6 = 8 2/3
Solve for -1 = -1/3 -3 = -3 1/3
Solution = 8 2/3 - -(3 1/3) = 12

OK so... I realized after typing the above that I missed the double-negative in the middle, I originally came out with 5 1/3, but I see my 'simple' mistake. I'm going to post this anyway for anyone else that may have been confused by the math involved.

I've got your code compiling, but I get errors all over the place when it tries to do the parsing you've programmed. I will continue looking at this and see if I can figure something out for you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 24
Reputation: AnnA.B is an unknown quantity at this point 
Solved Threads: 0
AnnA.B's Avatar
AnnA.B AnnA.B is offline Offline
Newbie Poster

Re: Integration

 
0
  #9
Jul 21st, 2009
squeege321, you'r correct

I will write another example, just in case, I missed something when I look to your calculation:
This the 'formula' how to solve definite integral:
<br />
        \int_a^b f(x)\, dx = F(b) - F(a)<br />

Example:
<br />
\int_1^2 (x^3+2x^2+4)\, dx = (\frac{x^4}{4}+\frac{2x^3}{3}+4x) \mid _1 ^2 = (\frac{16}{4}+\frac{2\cdot 8}{3}+8-\frac{1}{4}-\frac{2}{3}-4) = \frac{149}{12}<br />

If I want to write this in Delphi, I would write to console: integrate(x^3+2x^2+4,1,2)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Integration

 
0
  #10
Jul 21st, 2009
Yes well that is about as trivial as integration gets. The problem is when you want to consider trig functions etc.

I'm not sure exactly what you wish to do.

Do you want to enter ANY trivial expression at the command line? If that is the case then you have lots of issues.

1) You will have to write a parser (choices include RPN stacks, or building expression trees) to store your expression.
2) Once having stored that expression you can then find the integral by either using numerical methods such as the trapezium rule/Simpson's rule. This is the most easiest solution as you can apply this to more complicated functions with trigonometric values as long as you have lower and upper bound values.

The other solution is to now write your own univariate polynomial class to integrate your function. On top of that you will have to write ANOTHER parser to separate each term into its coefficient and exponent.


The easiest solution to this problem is to hard code the function in the program and then to evaluate an approximate solution using the trapezium rule. - after all integration is simply finding the area under the graph for that function given definite lower and upper bounds.

There's your food for thought.
Last edited by iamthwee; Jul 21st, 2009 at 7:02 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC