Methods in C version Toma 0.0.1
3 posters
IITDU Forum :: Academics (BIT) :: Semester 3 :: MATH 304
Page 1 of 1
Methods in C version Toma 0.0.1
CHAPTER 6:
6.6. Bisection Method:
6.7. False Position Method:
6.8. Newton-Raphson Method:
6.9. Secant Method:
6.10. Fixed-Point Method:
6.6. Bisection Method:
- Code:
#include<stdio.h>
#include<math.h>
double f(double x);
int main(void){
double a=-2, b=-1, c, fa = f(a), fb = f(b), fc, e = .000001;
int count=0;
if(fa*fb>0){
printf("Root is not in range\n");
return 0;
}
do{
c=(a+b)/2;
fc = f(c);
if(fa*fc
b = c;
fb = fc;
}
else{
a = c;
fa = fc;
}
count++;
}while(fabs(b-a)>e);
printf("The root is %f & solved by %d iterations\n", c, count);
return 0;
}
double f(double x){
return (x*x) - (4*x) -10;
}
6.7. False Position Method:
- Code:
#include<stdio.h>
#include<math.h>
double f(double x);
int main(void){
double a=1, b=3, c, fa = f(a), fb = f(b), fc, e = .000001;
int count=0;
if(fa*fb>0){
printf("Root is not in range\n");
return 0;
}
do{
c = a - fa*((b-a)/(fb-fa));
fc = f(c);
if(fa*fc
b = c;
fb = fc;
}
else{
a = c;
fa = fc;
}
count++;
}while(fabs(fc)>e);
printf("The root is %f & solved by %d iterations\n", c, count);
return 0;
}
double f(double x){
return (x*x) - x - 2;
}
6.8. Newton-Raphson Method:
- Code:
#include<stdio.h>
#include<math.h>
double f(double x);
double f_(double x);
int main(void){
double a=0, b, fa=f(a), f_a=f_(a), fb, e=.000001;
int count=0;
do{
b = a - (fa/f_a);
fb = f(b);
a = b;
fa = fb;
f_a = f_(b);
count++;
}while(fabs(fb)>e);
printf("The root is %f & solved by %d iterations\n", b, count);
return 0;
}
double f(double x){
return (x*x) - (3*x) + 2;
}
double f_(double x){
return (2*x) - 3;
}
6.9. Secant Method:
- Code:
#include<stdio.h>
#include<math.h>
double f(double x);
int main(void){
double a=4, b=2, c, fa=f(a), fb=f(b), fc, e=.000001;
int count=0;
do{
c = ((fb*a)-(fa*b))/(fb-fa);
a = b;
fa = fb;
b = c;
fb = f(c);
count++;
}while(fabs(a-b)>e);
printf("The root is %f & solved by %d iterations\n", c, count);
return 0;
}
double f(double x){
return (x*x) - (4*x) - 10;
}
6.10. Fixed-Point Method:
- Code:
#include
#include
double f(double x);
double g(double x);
int main(void){
double a=0, b, c, e=.000001;
int count=0;
do{
b = g(a);
c = a;
a = b;
count++;
}while(fabs(b-c)>e);
printf("The root is %f & solved by %d iterations\n", b, count);
return 0;
}
double f(double x){//no need of this. only for understanding the function
return (x*x) + x - 2;
}
double g(double x){
return 2 - (x*x);
}
Last edited by BIT0122-Amit on Fri May 21, 2010 1:46 pm; edited 2 times in total (Reason for editing : Better descriptive title :p)
BIT0107-Toma- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 280
Points : 453
Re: Methods in C version Toma 0.0.1
CHAPTER 8:
8.2. Jacobi Iteration Method:
8.3. Guass-Seidel Method:
8.4. Method of Relaxation:
I have not done it coz I dont know what to assume the value of r [You must be registered and logged in to see this image.] .
8.2. Jacobi Iteration Method:
- Code:
#include<stdio.h>
#include<math.h>
int main(void){
int n=3;
double x0[3], x[3], b[3] = {5, 15, 8}, e=.000001;
double a[3][3] = {
2, 1, 1,
3, 5, 2,
2, 1, 4
};
int count=0;
int i, j;
for(i=0; i<n; i++){
x0[i] = b[i]/a[i][i];
}
int isError=0;
do{
isError = 0;
for(i=0; i<n; i++){
double sum = b[i];
for(j=0; j<n; j++)
if(i!=j)
sum = sum - a[i][j]*x0[j];
x[i] = sum/a[i][i];
if(isError==0 && fabs(x[i]-x0[i])>e){
isError = 1;
x0[i] = x[i];
}
}
count++;
}while(isError);
printf("Roots are: ");
for(i=0; i<n; i++)
printf("%f ", x[i]);
printf("& solved by %d iterations\n", count);
return 0;
}
8.3. Guass-Seidel Method:
- Code:
#include<stdio.h>
#include<math.h>
int main(void){
int n = 3;
double x[3], b[3] = {5, 15, 8}, e=.000001;
double a[3][3] = {
2, 1, 1,
3, 5, 2,
2, 1, 4
};
int count=0;
int i, j;
for(i=0; i<n; i++)
x[i] = b[i]/a[i][i];
int isError;
do{
isError=0;
for(i=0; i<n; i++){
double sum = b[i];
for(j=0; j<n; j++)
if(i!=j)
sum = sum - a[i][j]*x[j];
double dummy = sum/a[i][i];
if(isError==0 && fabs(dummy-x[i])>e)
isError = 1;
x[i] = dummy;
}
count++;
}while(isError);
printf("Roots are: ");
for(i=0; i<n; i++)
printf("%f ", x[i]);
printf("& solved by %d iterations\n", count);
return 0;
}
8.4. Method of Relaxation:
I have not done it coz I dont know what to assume the value of r [You must be registered and logged in to see this image.] .
BIT0107-Toma- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 280
Points : 453
Re: Methods in C version Toma 0.0.1
its exactly same with gauss seidel method... whatever.... the value of here should be inside 1 and 2.. try with setting a value of r as 1.5
BIT0101-rayhan- Release Candidate
- Posts : 107
Points : 177
Similar topics
» Methods Covered in Math 304
» Toma's Disting Dance
» AssaultCube Version 1.1
» Turbo C++ (ancient DOS version)
» Yahoo Messenger 10 Beta version
» Toma's Disting Dance
» AssaultCube Version 1.1
» Turbo C++ (ancient DOS version)
» Yahoo Messenger 10 Beta version
IITDU Forum :: Academics (BIT) :: Semester 3 :: MATH 304
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford