Register - Login
Views: 99858813
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - Stats - Latest Posts - Color Chart - Smilies
05-04-22 12:32:22 PM
Jul - Computers and Technology - Euler's Method in C++ New poll - New thread - New reply
Next newer thread | Next older thread
Cirvante
1340
Feel the wrath of eternal damnation please! I would appreciate that very much thank you!
Level: 74


Posts: 641/1342
EXP: 3616833
For next: 36711

Since: 07-10-07


Since last post: 8.4 years
Last activity: 6 days

Posted on 02-23-09 08:59:09 PM (last edited by Cirvante at 02-24-09 02:18 PM) Link | Quote
Euler's Method for approximating the solution to an initial value problem in differential equations, where:

h is the step size,
y0=y(0)=initial value,
yn=y(n*h)=value you want to estimate, then

yn=yn-1 + hF(xn-1,yn-1)



#include <iostream.h>

#include <math.h>

double euler (double h, double y, double n)

int main (void)
{
double a, b, c;
int n;

cout<<"Value of h: "; //Step size
cin>>a;
cout<<"Initial value y(0): "; //Initial value
cin>>b;
cout<<"Value to be estimated y(x): "; //ex: If estimating y(1), write 1
cin>>c;

n=c/a; //Number of necessary iterations (int)

printf("%3.8f\n", euler(a,c,n))

system("pause") //Only there so Windows doesn't retardedly close the window immediately after execution
return 0;
}

double euler (double h, double y, double n) //Does the actual work
{
int i=0;
double x=0;

while (i <= c)
{
y=y+h*(2*x*y) //Whatever's in parentheses is F(x,y), change at will
x+=h;
i++;
}

return y;
}

Example: A step size of 0.2, an initial value of 1 and estimating y(1) returns 2.05058.



This is something I came up with after our Calc II professor was so kind to include a Euler approximation with like 0.05 step size to 6 significant digits as part of our assignment. Realizing it would take me far less time to program a solution instead of solving by hand and writing down each iteration, I came up with this. He's really anal about precision, though, so I'd appreciate any help with making this more accurate (setting everything to double instead of float doesn't get me any more decimal places)

____________________
board2 - Jul - OC ReMix
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 433/603
EXP: 1084819
For next: 72300

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 02-23-09 09:07:51 PM (last edited by neotransotaku at 02-23-09 07:03 PM) Link | Quote
Originally posted by Cirvante
This is something I came up with after our Calc II professor was so kind to include a Euler approximation with like 0.05 step size to 6 significant digits as part of our assignment. Realizing it would take me far less time to program a solution instead of solving by hand and writing down each iteration, I came up with this. He's really anal about precision, though, so I'd appreciate any help with making this more accurate (setting everything to double instead of float doesn't get me any more decimal places)


while it doesn't give you anymore decimal places, using double versus float may make a difference as to how those smaller bits not found in float influence the larger fractional ends. After all, you want to minimize any controllable error.
Sine
2310
Level: 94


Posts: 1222/2316
EXP: 8201547
For next: 155110

Since: 07-07-07


Since last post: 3.7 years
Last activity: 290 days

Posted on 02-23-09 09:37:19 PM Link | Quote

Shiny new layout!
----------------------------------------------------

If you want I can give you what I wrote last year for Calc BC

Fits on your TI 83/89

____________________
The question is what is the question
Cirvante
1340
Feel the wrath of eternal damnation please! I would appreciate that very much thank you!
Level: 74


Posts: 644/1342
EXP: 3616833
For next: 36711

Since: 07-10-07


Since last post: 8.4 years
Last activity: 6 days

Posted on 02-23-09 11:33:17 PM Link | Quote
I'm actually very tempted to hand in this instead of ahowing every iteration by hand, just to see how the professor reacts.

It's a lot shorter, too.

Also, Katty, that would be very nice of you

____________________
board2 - Jul - OC ReMix
Hiryuu

Level: 207


Posts: 7110/14435
EXP: 127636521
For next: 2147633

Since: 07-06-07


Since last post: 11.8 years
Last activity: 11.7 years

Posted on 02-24-09 06:57:04 AM Link | Quote
Nerds. NERRRRRRRRRRRRRDSSSSSSSSS!

____________________
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 435/603
EXP: 1084819
For next: 72300

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 02-24-09 02:35:05 PM Link | Quote
Otaku! OTAAAAAAAAAAAAAKUUUUUUUUU!
Hiryuu

Level: 207


Posts: 7121/14435
EXP: 127636521
For next: 2147633

Since: 07-06-07


Since last post: 11.8 years
Last activity: 11.7 years

Posted on 02-24-09 04:17:36 PM Link | Quote
D...don't try that! Your common sense does nothing to me!

____________________
Cirvante
1340
Feel the wrath of eternal damnation please! I would appreciate that very much thank you!
Level: 74


Posts: 645/1342
EXP: 3616833
For next: 36711

Since: 07-10-07


Since last post: 8.4 years
Last activity: 6 days

Posted on 02-24-09 05:24:46 PM (last edited by Cirvante at 02-24-09 02:25 PM) Link | Quote
Revised for precision. Changed all the floats to doubles and replaced the cout with a printf() because apparently cout fails at printing more than six decimal places

h=0.2, y0=1 and y(1) now gives 2.05058304.

Did I mention Euler approximations are horribly, horribly inefficient? You need a step size in the order of 10-7 to get a sufficiently accurate approximation to the true solution, which is e (2.718281828...)

____________________
board2 - Jul - OC ReMix
Sine
2310
Level: 94


Posts: 1224/2316
EXP: 8201547
For next: 155110

Since: 07-07-07


Since last post: 3.7 years
Last activity: 290 days

Posted on 02-24-09 05:56:48 PM (last edited by Kattwah at 02-24-09 02:57 PM) Link | Quote

Shiny new layout!
----------------------------------------------------

So yeah...




euler()

Disp "equation:"
InputStr m
Define f(t,y) = expr(m)

Disp "ti, yi: "
Input a
Input b
Disp "end t: "
Input z
Disp "(delta)x: "
Input h

For i,a,z,h

Disp [[a,b,f(a,b)]]

b+f(a,b)*h -> b
a+h->a

EndFor
EndPrgm

I should really write that as a function.

____________________
The question is what is the question
Tina
Beep boop
Level: 79


Posts: 461/1549
EXP: 4472116
For next: 107351

Since: 08-10-07


Since last post: 3.4 years
Last activity: 3.4 years

Posted on 02-25-09 07:37:33 PM Link | Quote
Originally posted by Cirvante
Changed all the floats to doubles and replaced the cout with a printf() because apparently cout fails

There was some excess in that sentence


Kattwah: What language is that?

____________________
Hiryuu

Level: 207


Posts: 7161/14435
EXP: 127636521
For next: 2147633

Since: 07-06-07


Since last post: 11.8 years
Last activity: 11.7 years

Posted on 02-25-09 07:38:36 PM Link | Quote
If I didn't know better, I'd say TI-89.

____________________
Tina
Beep boop
Level: 79


Posts: 463/1549
EXP: 4472116
For next: 107351

Since: 08-10-07


Since last post: 3.4 years
Last activity: 3.4 years

Posted on 02-25-09 07:44:21 PM Link | Quote
That's actually very possible; it's been quite some time since I've done anything in the TI-92+ language.

____________________
Tiden
Disgusting human being, just awful
Level: NaN


Posts: 3478/-6493
EXP: NaN
For next: 0

Since: 07-03-07

Pronouns: they/them

Since last post: 4.1 years
Last activity: 3.6 years

Posted on 02-25-09 08:12:00 PM Link | Quote
I got bored and wrote a python version of that:

from decimal import *

D = Decimal
getcontext().prec = 12
def euler(h, y, n):
i = n / h
x = Decimal("0.0")
for i in xrange(0,i):
y = y + h * (2 * x * y)
x += h
return y

print euler(D("0.2"), 1, D("1"))


And the output:

C:\Documents and Settings\hydrapheetz\Desktop>python eulerm.py

2.0505830400



I'll probably write it in a few other languages later when I get the chance.
Next newer thread | Next older thread
Jul - Computers and Technology - Euler's Method in C++ New poll - New thread - New reply


Rusted Logic

Acmlmboard - commit 47be4dc [2021-08-23]
©2000-2022 Acmlm, Xkeeper, Kaito Sinclaire, et al.

29 database queries, 1 query cache hits.
Query execution time: 0.086792 seconds
Script execution time: 0.033958 seconds
Total render time: 0.120750 seconds