Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,26 +1,22 @@
|
|||
import std.stdio, std.range;
|
||||
import std.stdio, std.range, std.traits;
|
||||
|
||||
/**
|
||||
Approximates y(t) in y'(t)=f(t,y) with y(a)=y0 and
|
||||
t=a..b and the step size h.
|
||||
*/
|
||||
void euler(F)(in F f, in double y0,
|
||||
in double a, in double b, in double h) {
|
||||
/// Approximates y(t) in y'(t)=f(t,y) with y(a)=y0 and t=a..b and the step size h.
|
||||
void euler(F)(in F f, in double y0, in double a, in double b, in double h) @safe
|
||||
if (isCallable!F && __traits(compiles, { real r = f(0.0, 0.0); })) {
|
||||
double y = y0;
|
||||
foreach (t; iota(a, b, h)) {
|
||||
foreach (immutable t; iota(a, b, h)) {
|
||||
writefln("%.3f %.3f", t, y);
|
||||
y += h * f(t, y);
|
||||
}
|
||||
writeln("done");
|
||||
"done".writeln;
|
||||
}
|
||||
|
||||
void main() {
|
||||
/// Example: Newton's cooling law
|
||||
static newtonCoolingLaw(in double time, in double t) {
|
||||
return -0.07 * (t - 20);
|
||||
}
|
||||
/// Example: Newton's cooling law.
|
||||
enum newtonCoolingLaw = (in double time, in double t)
|
||||
pure nothrow @safe @nogc => -0.07 * (t - 20);
|
||||
|
||||
euler(&newtonCoolingLaw, 100, 0, 100, 2);
|
||||
euler(&newtonCoolingLaw, 100, 0, 100, 5);
|
||||
euler(&newtonCoolingLaw, 100, 0, 100, 10);
|
||||
euler(newtonCoolingLaw, 100, 0, 100, 2);
|
||||
euler(newtonCoolingLaw, 100, 0, 100, 5);
|
||||
euler(newtonCoolingLaw, 100, 0, 100, 10);
|
||||
}
|
||||
|
|
|
|||
29
Task/Euler-method/JavaScript/euler-method.js
Normal file
29
Task/Euler-method/JavaScript/euler-method.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Function that takes differential-equation, initial condition,
|
||||
// ending x, and step size as parameters
|
||||
function eulersMethod(f, x1, y1, x2, h) {
|
||||
// Header
|
||||
console.log("\tX\t|\tY\t");
|
||||
console.log("------------------------------------");
|
||||
|
||||
// Initial Variables
|
||||
var x=x1, y=y1;
|
||||
|
||||
// While we're not done yet
|
||||
// Both sides of the OR let you do Euler's Method backwards
|
||||
while ((x<x2 && x1<x2) || (x>x2 && x1>x2)) {
|
||||
// Print what we have
|
||||
console.log("\t" + x + "\t|\t" + y);
|
||||
|
||||
// Calculate the next values
|
||||
y += h*f(x, y)
|
||||
x += h;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
function cooling(x, y) {
|
||||
return -0.07 * (y-20);
|
||||
}
|
||||
|
||||
eulersMethod(cooling, 0, 100, 100, 10);
|
||||
17
Task/Euler-method/R/euler-method.r
Normal file
17
Task/Euler-method/R/euler-method.r
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
euler <- function(f, y0, a, b, h)
|
||||
{
|
||||
t <- a
|
||||
y <- y0
|
||||
|
||||
while (t < b)
|
||||
{
|
||||
cat(sprintf("%6.3f %6.3f\n", t, y))
|
||||
t <- t + h
|
||||
y <- y + h*f(t, y)
|
||||
}
|
||||
}
|
||||
|
||||
newtoncooling <- function(time, temp)
|
||||
return(-0.07*(temp-20))
|
||||
|
||||
euler(newtoncooling, 100, 0, 100, 10)
|
||||
Loading…
Add table
Add a link
Reference in a new issue