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
AnnA.B 13
- 4 Contributors
- forum15 Replies
- 21 Views
- 9 Years Discussion Span
- comment Latest Post by FlamingClaw
FlamingClaw 98
I didn't understand everything...
You said,the function made by you...
Can you send some code?Too less information about your function....
AnnA.B 13
There are two functions, one is for 'reading' input, and second one is for calculation. Here is code:
function RightStr(const AStr: String; Len: Integer): String;
begin
end;
function LeftStr(const AText: string; const ACount: Integer): string;
begin
end;
function izraz (s1: string): string;
var s2 : string;
t,s: array[1..30] of char;
j: integer;
factor,potency,posx,pos_pot: integer;
polinom: array[0..10] of integer;
begin
repeat
posx := pos('x',s2); //find x
if posx > 1 then // if we find x, then read characters before x
begin
j:=1;
repeat
t[j] := s2[j];
j:=j+1;
until ( s2[j] = 'x' ) ;
factor := strtoint(t);
end
else // if x is on the first place, then factor is 1
begin
if posx = 1 then
factor := 1
else
begin
// t := niz1;
s2 := '';
factor := strtoint(t);
end;
end;
if posx >= 1 then // if x is found, search ^
begin
pos_pot := pos('^',s2);
if pos_pot > 1 then
begin
s2 := rightstr(s2,length(s2)-pos_pot);
j:=1;
repeat
s[j] := s2[j];
j:=j+1;
until ( s2[j] = '+' ) or ( s2[j] = '-' ) or ( j>length(s2) ) ;
potency := strtoint(s);
s2 := rightstr(s2,length(s2)-j+1);
end
else
begin
potency := 1;
s2 := rightstr(s2,length(s2)-posx);
end;
end
else
potency := 0;
polinom[potency] := factor;
until s2 = '';
end;
function integral(input: string): extended;
var p,i, t2 : integer;
niz1: string;
l: integer;
lower_limit, upper_limit: string[2];
begin
delete(input,1,9);
delete(input,length(input),1);
p:=pos(',',input);
niz1:=copy(input,1,p-1);
izraz(niz1);
l:=length(input);
t2:=l-2;
begin
if input[l-3]='-' then
upper_limit:=copy(input,length(input[i-3]),2);
if input[i-6]='-' then
lower_limit:=copy(input,length(input[i-6]),2)
else
lower_limit:=inttostr(l-5);
if input[l-3]<>'-' then
upper_limit:=inttostr(l-2);
if input[l-5]='-' then
lower_limit:=copy(input,length(input[i-5]),2)
else
lower_limit:=inttostr(l-4);
end;
end;
FlamingClaw 98
I want to understand but... my efforts wasn't success :'(
function integral(input: string): extended;//WAITS STRING ,BACK EXTENDED
var p,//STORES THE ',' POSITION IN THE *INPUT* STRING
i, t2 : integer;
niz1: string;
l: integer;
lower_limit, upper_limit: string[2];
begin
{
procedure Delete(var S: String; Index: Integer; Count:Integer);
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.
}
delete(input,1,9);//DELETE(INPUT,from 1ST,9 CHARS)
delete(input,length(input),1);//DELETE(INPUT,length of input,1 char)
{AFTER THE TWO DELETE PROCEDURE THE *INPUT*'S VALUE IS EMPTY!!!!!}
p:=pos(',',input);//HOW CAN IT FIND IN A EMPTY STRING????
{
SO WHEN I TRY TO USE YOUR FUNCTION THIS IS NOT WORKING AT ALL,AND WHERE IS THE BACK VALUE
Integral:=?????? this is important part of a function....
What do you want? Integrate a function to another?
We need 'back value' to integrate them...
}
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....
AnnA.B 13
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
iamthwee 1,547
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.
AnnA.B 13
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.
squeege321
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.
AnnA.B 13
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:
\int_a^b f(x)\, dx = F(b) - F(a)
Example:
\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}
If I want to write this in Delphi, I would write to console: integrate(x^3+2x^2+4,1,2)
Edited
by Reverend Jim: Fixed formatting
iamthwee 1,547
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.
AnnA.B 13
I want to integrate only polynoms like that:
[TEX] p_n (x) = a_n x^n + a_{n - 1} x^{n - 1} + a_{n - 2} x^{n - 2} + \cdots + a_2 x^2 + a_1 x + a_0\qquad[/TEX]
Here we use the basic rule to integrate: [TEX] \int x^n\,dx = \frac{x^{n+1}}{n+1} \qquad\mbox{ where }n \ne -1[/TEX]
In this program, there will be no hyperbolic functions, no logarithms, no exponential functions, no trigonometric functions, just polynoms.
iamthwee 1,547
Okay so...
- Do you wish to enter the expression at the command line?
- Will you definitely be given the upper and lower bounds?
- Are you planning to find the exact answer or an approximate anser?
- An approximate answer implies the use of numerical methods.
AnnA.B 13
Okay so...
* Do you wish to enter the expression at the command line? yes
* Will you definitely be given the upper and lower bounds? yes
* Are you planning to find the exact answer or an approximate anser? I'm planing to find approximate answer, so numerical methods should be used
iamthwee 1,547
OK
Here's the good news. The answers to the last two questions makes this doable. - hence the trapezium rule.
The bad news, because you are inputting the expression at the command line you will have to write a parser or an eval() function.
I don't use delphi but I came across
http://delphi.icm.edu.pl/ftp/d30share/eval.htm
Good luck
AnnA.B 13
iamthwee, thanks for wishing me luck, I will need it! I'm doing on this, but I think I'll give up I can't get the right idea to solve this...
FlamingClaw 98
Sorry this thread too big ball to me :'( ,but when I shearched the net about polynoms I found a page where you can calucalte with them...
http://xrjunque.nom.es/precis/polycalc.aspx?ln=en :D