find the (approximate) value of y(0.5) for dy/dt = t2 + y2, y(0)=1 on the interval [0, 0.5] in javascript

index.tsx
// Define the differential equation dy/dt = t^2 + y^2
function dydt(t, y) {
    return Math.pow(t, 2) + Math.pow(y, 2);
}

// Define the Euler's method to approximate y(0.5)
function eulerMethod(dydt, t0, y0, h, tEnd) {
    let t = t0;
    let y = y0;
    
    while (t < tEnd) {
        y = y + h * dydt(t, y);
        t = t + h;
    }
    
    return y;
}

// Initial values
let t0 = 0;
let y0 = 1;
let h = 0.1;
let tEnd = 0.5;

// Approximate y(0.5) using Euler's method
let approxValue = eulerMethod(dydt, t0, y0, h, tEnd);
console.log(approxValue);
554 chars
28 lines

Output: Approximate value of y(0.5) is calculated and displayed in the console using Euler's method.

index.tsx
0 chars
1 lines

gistlibby LogSnag