목차
1. Source code
2. result
2. result
본문내용
%f\n",p2);
while(absol(p2-p1)>=TOL)
{
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
n++;
printf("n= %d Pn= %f f(Pn)= %f \n",n,p2,f(p2));
}
printf("Solution is %f\n\n",p2);
return 0;
}
(2)Result
HW#7
2.4-9
(1) Source code
/*
I sketched the graph of f(x)=x^2 -3 and I concluded that
I will start the Newton's method at p=2
for the value of x with x= sqrt(3)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
double newton(double p2);
main()
{
newton(2);
return 0;
}
double f(double x)
{
return x*x - 3;
}
double fp(double x)
{
return 2*x;
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
double newton(double p2)
{
int n;
double p1;
p1 = -1;
n = 0;
printf("Now we are doing Newton's method at %f\n",p2);
while(absol(p2-p1)>=TOL)
{
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
n++;
printf("n= %d Pn= %f f(Pn)= %f \n",n,p2,f(p2));
}
printf("Solution is %f\n\n",p2);
return 0;
}
(2)Result
(3)Compare to 2.2-9 and 2.3-9
( Bisection method )
( Secant method )
I used the Bisection method for excercise 2.2-9, and
I used the Secant method for excercise 2.3-9.
I used the Newton's method for excercise 2.3-9.
The number of iteration was different one another.
- n -
Bisection method : 14 times
Secant method : 5 times
Newton's method : 3 times
Therefore, Newton's method is more efficient than any other methods.
HW#8
2.5-2
a.
(1) Source code
/*
I will start the Newton's method in [0,1]
for the value of x with x^2-2*x*e^(-x)+e^(-2*x)=0
using Aitken's delta^2 method.
Iterate until |qn - qn-1|<10^(-4)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
main()
{
int n;
double p0,p1,p2,q1,q2;
p0 = p1 = p2 = q1 = 0;
q2 = 1;
n = 0;
while(absol(q2-q1)>=TOL)
{
q1 = q2;
p0 = p1;
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
q2 = p0 - (p1 - p0)*(p1 - p0)/(p2 - 2*p1 + p0);
n++;
printf("n= %d Pn= %f qn = %f \n",n,p2,q2);
}
printf("Solution is %f\n\n",q2);
return 0;
}
double f(double x)
{
return x*x-2*x*exp(-x)+exp(-2*x);
}
double fp(double x)
{
return 2*x+(2*x-2)*exp(-1*x)-2*exp(-2*x);
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
(2)Result
b.
(1) Source code
/*
I will start the Newton's method in [-2,-1]
for the value of x with cos(x+2^(1/2))+x*(1/2*x+2^(1/2))=0
using Aitken's delta^2 method.
Iterate until |qn - qn-1|<10^(-4)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
main()
{
int n;
double p0,p1,p2,q1,q2;
p0 = p1 = p2 = q1 = -1;
q2 = -2;
n = 0;
while(absol(q2-q1)>=TOL)
{
q1 = q2;
p0 = p1;
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
q2 = p0 - (p1 - p0)*(p1 - p0)/(p2 - 2*p1 + p0);
n++;
printf("n= %d Pn= %f qn = %f \n",n,p2,q2);
}
printf("Solution is %f\n\n",q2);
return 0;
}
double f(double x)
{
return cos(x+sqrt(2))+x*(x/2+sqrt(2));
}
double fp(double x)
{
return x+sqrt(2)-sin(x+sqrt(2));
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
(2)Result
while(absol(p2-p1)>=TOL)
{
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
n++;
printf("n= %d Pn= %f f(Pn)= %f \n",n,p2,f(p2));
}
printf("Solution is %f\n\n",p2);
return 0;
}
(2)Result
HW#7
2.4-9
(1) Source code
/*
I sketched the graph of f(x)=x^2 -3 and I concluded that
I will start the Newton's method at p=2
for the value of x with x= sqrt(3)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
double newton(double p2);
main()
{
newton(2);
return 0;
}
double f(double x)
{
return x*x - 3;
}
double fp(double x)
{
return 2*x;
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
double newton(double p2)
{
int n;
double p1;
p1 = -1;
n = 0;
printf("Now we are doing Newton's method at %f\n",p2);
while(absol(p2-p1)>=TOL)
{
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
n++;
printf("n= %d Pn= %f f(Pn)= %f \n",n,p2,f(p2));
}
printf("Solution is %f\n\n",p2);
return 0;
}
(2)Result
(3)Compare to 2.2-9 and 2.3-9
( Bisection method )
( Secant method )
I used the Bisection method for excercise 2.2-9, and
I used the Secant method for excercise 2.3-9.
I used the Newton's method for excercise 2.3-9.
The number of iteration was different one another.
- n -
Bisection method : 14 times
Secant method : 5 times
Newton's method : 3 times
Therefore, Newton's method is more efficient than any other methods.
HW#8
2.5-2
a.
(1) Source code
/*
I will start the Newton's method in [0,1]
for the value of x with x^2-2*x*e^(-x)+e^(-2*x)=0
using Aitken's delta^2 method.
Iterate until |qn - qn-1|<10^(-4)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
main()
{
int n;
double p0,p1,p2,q1,q2;
p0 = p1 = p2 = q1 = 0;
q2 = 1;
n = 0;
while(absol(q2-q1)>=TOL)
{
q1 = q2;
p0 = p1;
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
q2 = p0 - (p1 - p0)*(p1 - p0)/(p2 - 2*p1 + p0);
n++;
printf("n= %d Pn= %f qn = %f \n",n,p2,q2);
}
printf("Solution is %f\n\n",q2);
return 0;
}
double f(double x)
{
return x*x-2*x*exp(-x)+exp(-2*x);
}
double fp(double x)
{
return 2*x+(2*x-2)*exp(-1*x)-2*exp(-2*x);
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
(2)Result
b.
(1) Source code
/*
I will start the Newton's method in [-2,-1]
for the value of x with cos(x+2^(1/2))+x*(1/2*x+2^(1/2))=0
using Aitken's delta^2 method.
Iterate until |qn - qn-1|<10^(-4)
*/
#include
#include
#define TOL 0.0001
double f(double x);
double fp(double x);
double absol(double x);
main()
{
int n;
double p0,p1,p2,q1,q2;
p0 = p1 = p2 = q1 = -1;
q2 = -2;
n = 0;
while(absol(q2-q1)>=TOL)
{
q1 = q2;
p0 = p1;
p1 = p2;
p2 = p1 - f(p1)/fp(p1);
q2 = p0 - (p1 - p0)*(p1 - p0)/(p2 - 2*p1 + p0);
n++;
printf("n= %d Pn= %f qn = %f \n",n,p2,q2);
}
printf("Solution is %f\n\n",q2);
return 0;
}
double f(double x)
{
return cos(x+sqrt(2))+x*(x/2+sqrt(2));
}
double fp(double x)
{
return x+sqrt(2)-sin(x+sqrt(2));
}
double absol(double x)
{
if(x>=0)return x;
else return (-1) * x;
}
(2)Result
소개글