# include <iostream> /// This code is right protected , by Dastegir Sir ///
# include <stdio.h>
# include <stdlib.h>
# include <cmath>
# include <math.h>
using namespace std;
#define NUM 10000
double simp (double(*func) (double),double a,double b,int N)
{
int i=0;
double sum=0.0,h=0.0,x=a,ff=0.0,fm=0.0,fl=0.0;
h=(b-a)/(double) N;
for (i=1;i<=N/2;i++){
ff=(*func)(x);
fm=(*func)(x+2*h);
fl=4.0*((*func)(x+h));
x+=2*h;
sum+=(ff+fm+fl);
}
sum*=(h/3.0);
return sum;
}
double func1(double x) { return cos(M_PI*x*x/2);}
double func2(double x) {return sin(M_PI*x*x/2);}
double knedge(double u0, double I0)
{
double val1=0.0, val2=0.0;
val1 = simp(func1,0.0, u0, NUM );
val2 = simp(func2,0.0, u0, NUM );
return 0.5*I0* ( (val1+0.5)*(val1+0.5) + (val2+0.5)*(val2+0.5) );
}
int main()
{
int j=0;
double Ival=0.0, u0=0.0, I0=0.0;
for(u0=-1.0; u0<=4.0; u0 += 0.01)
{
Ival =knedge(u0,1.0);
printf("%14.12lf %14.12lf \n",u0,Ival);
}
return 0;
}
2.0 NaCl.cpp
#include<iostream>
#include<stdio.h> /// This code is right protected , by Dastegir Sir ///
#include<stdlib.h>
#include<math.h>
using namespace std;
double nacl(double (*func)(double x),double a,double b, int N, double epsilon,int *m)
{
int i=0;
double val=0.0, c=0.0;
if ((*func)(a) * (*func)(b) >0.0)
{
printf("root not brackets\n");
exit(1);
}
if (fabs ( (*func) (a) * (*func) (b)) <= epsilon)
{
printf("Either a or b is a good enough root\n");
if(fabs ( (*func) (a) <=epsilon ))
{
printf("a is a root\n");
return a;
}
else{return b;}
}
for(i=0; i<=N; i+=1) //this for doesn't work that why i just get output only 10,i also change the for loop but there is no change.
{
*m = *m+1;
c=0.5 * (a+b);
if((*func)(a) * (*func) (c) <0.0)
b=c;
else a=c;
if(fabs ( (*func)(c))<epsilon)
{return c;}
}
cout<< "Maximum number of iterations exceed"<<endl;
return c;
}
double func1 (double x) {return(-132.276/(x*x) ) + 3303.03 * exp(-x); }
int main( )
{
int N=0,m=0;
double a=0.0, b= M_PI, epsilon=1.0e-5, val = 0.0;
N=1000000;
val = nacl (func1,a,b,N,epsilon, &m) ;
printf("val = %19.16lf m=%d \n",val,m);
return 0;
}
3.0 numerov.cpp
#include<iostream>
#include<stdio.h> /// This code is right protected , by Dastegir Sir ///
#include<stdlib.h>
#include<math.h>
using namespace std;
#define L 4.0
#define A 1.0
#define nn 10
double numerov (double (*k2) (double) , double x0, double xf, int N, double y0, double y1 )
{
int i=0;
double x=0.0, y=0.0, yp=0.0, h=0.0, ytmp=0.0;
//parameter value
h=( xf - x0 ) / (double) N;
//Initial conditions
x=x0+h;
y=y1;
yp=y0;
for( i=0; i<=N; i++ )
{
if( i == 0 ) { y=y0; x=x0; printf("%lf %12.10lf\n", x, y ); }
else if (i == 1) { y=y1; x=x0+h; printf("%lf %12.10lf\n", x, y ); }
else
{
ytmp = y;
y = 2.0 * ( 1.0-5.0 *h*h*(*k2)(x)/12.0 ) * y ;
y - = ( 1.0+ h*h*(*k2)(x-h) / 12.0) * yp;
y / = ( 1.0 + h*h*(*k2)(x+h)/12.0);
printf("%lf %12.10lf\n", x, y );
yp= ytmp;
x+ = h;
}
}
return y;
}double ksqrt(double x)
{
return M_PI * M_PI*nn*nn/(L*L);
}
int main ( ) {
int N = 0, j= 0 ;
double x = 0.0, y = 0.0, x0 = 0.0, xf = L, y0 = 0.0, y1 = 0.0, h = 0.0, yp = 0.0, ytmp = 0.0;
double kn =0.0;
//parameter values
x0 =0.0;
xf = 4.0;
N = 1000;
h = (xf-x0) / (double)N;
kn = nn*M_PI / ( xf - x0 );
//Initial conditions
x = x0;y0=0.0;
y1=y0 + h*kn*A*cos( kn*x0 ) - h*h*kn*kn*A*sin( kn*x0 );
yp = y0;
y = y1;
y = numerov ( ksqrt, x, xf, N, yp, y );
return 0;
}
http://2.bp.blogspot.com/-Nd8ntBaT5FY/Tt-D-SeD96I/AAAAAAAAAJo/4vWzE6gyT1A/s1600/prob11_sheet%2B2.jpg
ReplyDeleteLab Problem 11(RK4 method)problem given by Mobin...