Posts

Showing posts with the label Euler Method

Find the root of the equation using Newton reperson method by MistarAV

 #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> /* Defining equation to be solved. Change this equation to solve another problem. */ #define f(x) 3*x - cos(x) -1 /* Defining derivative of g(x). As you change f(x), change this function also. */ #define g(x) 3 + sin(x) void main() { clrscr (); float x0, x1, f0, f1, g0, e; int step = 1, N; /* Inputs */ printf("\nEnter initial guess:\n"); scanf("%f", &x0); printf("Enter tolerable error:\n"); scanf("%f", &e); printf("Enter maximum iteration:\n"); scanf("%d", &N); /* Implementing Newton Raphson Method */ printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n"); do { g0 = g(x0); f0 = f(x0); if(g0 == 0.0) { printf("Mathematical Error."); exit(0); } x1 = x0 - f0/g0; printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1); x0 = x1; step = step+1; if(step > N) { printf("Not Convergent."); exit(0); }

Write a C program on Numerical solution of ODE using Euler method by MistarAV

 #include<stdio.h> #include<conio.h> #define f(x,y) x+y int main() { clrscr(); float x0, y0, xn, h, yn, slope; int i, n; printf("Enter Initial Condition\n"); printf("x0 = "); scanf("%f", &x0); printf("y0 = "); scanf("%f", &y0); printf("Enter calculation point xn = "); scanf("%f", &xn); printf("Enter number of steps: "); scanf("%d", &n); /* Calculating step size (h) */ h = (xn-x0)/n; /* Euler's Method */ printf("\nx0\ty0\tslope\tyn\n"); printf("------------------------------\n"); for(i=0; i < n; i++) { slope = f(x0, y0); yn = y0 + h * slope; printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn); y0 = yn; x0 = x0+h; } /* Displaying result */ printf("\nValue of y at x = %0.2f is %0.3f",xn, yn); getch(); } Output Enter Initial Condition x0 = 0 y0 = 1 Enter calculation point xn = 1 Enter number of steps: 10 x0 y0 slope yn -------