gistlib
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 chars28 lines
// 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);
Output: Approximate value of y(0.5) is calculated and displayed in the console using Euler's method.
index.tsx0 chars1 lines
gistlibby LogSnag