Assignment 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

NC Assignment-01

Abdul Moeed

FA19-BCS-003

BCS-V(A)

QUESTION-1:

a.

REPORT TITLE PAGE 2


b.
#include <iostream>
#include <cmath>
using namespace std;
double fun(double);
double quadSecRoot(double (*fun)(double), double, double);
bool checkValidity(double, double);
double ERR = 1e-6;
int main() {
double a = 0.4, b = 0.6;
double solution = quadSecRoot(&fun, a, b);
cout << "therefore the solution is " << solution;
}
double quadSecRoot(double (*fun)(double), double a, double b){
if(!checkValidity(a, b)){
cout << "error in the intial values";
return 0;
}
double Xmid, Xfquad, Xlquad;
do{
Xmid = (a + b)/2;
Xfquad = (a + Xmid)/2;
Xlquad = (b + Xmid)/2;
if(fun(a) * fun(Xmid) < 0)
b = Xfquad;
else if(fun(Xfquad) * fun(Xmid) < 0) {
a = Xfquad;
b = Xmid;
}else if(fun(Xmid) * fun(Xlquad) < 0) {
a = Xmid;
b = Xlquad;
}else
a = Xlquad;
}while(abs(fun(Xmid) > ERR));
return Xmid;
}
double fun(double x) {
return cos(x) - 5*x + 2;
}
bool checkValidity(double a, double b){
return fun(a) * fun(b) < 0;
}

REPORT TITLE PAGE 3


QUESTION-2:

a.

REPORT TITLE PAGE 4


b.
#include <iostream>
#include <cmath>
using namespace std;
double fun(double);
double rfMethod(double, double);
bool checkValidity(double, double);
double ERR = 1e-6;
int main() {
double a = 0.4, b = 0.6;
double solution = rfMethod(a, b);
cout << "Solution: " << solution;
return 0;
}
double rfMethod(double a, double b){
if(!checkValidity(a, b)){
cout << "error in the intial values";
return 0;
}
double x;
do{
x = (a*(fun(b)) - b*(fun(a)))/(fun(b)-fun(a));
if(fun(x) == 0){
cout << "breaking at the first cond";
break;
}else if(fun(a) * fun(x) < 0)
b = x;
else
a = x;
}while(abs(fun(x) > ERR));
return x;
}
double fun(double x) {
return cos(x) - 5*x + 2;
}
bool checkValidity(double a, double b){
return fun(a) * fun(b) < 0;
}

REPORT TITLE PAGE 5


QUESTION-3:

a.

b.
#include <iostream>
#include <cmath>
using namespace std;
double fun(double);
double secMethod(double, double);
bool checkValidity(double, double);
double ERR = 1e-6;
int main() {
double a = 0.4, b = 0.5;
double solution = secMethod(a, b);
cout << "Solution: " << solution;
return 0;
}
double secMethod(double a, double b){
if(!checkValidity(a, b)){
cout << "error in the intial values";
return 0;

REPORT TITLE PAGE 6


}
double x;
do{
x = b - ((fun(b) * (b-a))/(fun(b) - fun(a)));
if(fun(x) == 0)
break;
a = b;
b = x;
}while(abs(fun(x)) > ERR);
return x;
}
double fun(double x) {
return 2*cosh(x) * sin(x) - 1;
}
bool checkValidity(double a, double b){
return fun(a) * fun(b) < 0;
}

REPORT TITLE PAGE 7


QUESTION-4:

a.

REPORT TITLE PAGE 8


b.
// Online C++ compiler to run C++ program online
#include <iostream>
#include <cmath>
using namespace std;
double fun(double);
double dFun(double);
double hybrid(double, double);
bool initValid(double, double);
double ERR = 1e-6;
int main() {
double a = 0, b = 1;
double solution = hybrid(a, b);
cout << "solution: " << solution;
}
double hybrid(double a, double b) {
if(!initValid(a, b)){
cout << "initial values are incorrect";
return 0;
}
double x, xn;
for(int i = 0; i < 2; i++) {
x = (a + b)/2;
if(fun(x) == 0 || fun(x) < 0)
return x;
else if(fun(a) * fun(x) < 0)
b = x;
else
a = x;
}
do {
xn = x - (fun(x) / dFun(x));
x = xn;
}while(fun(x) < ERR);
return x;
}
bool initValid(double a, double b){
return fun(a) * fun(b) < 0;
}
double fun(double x){
return pow(x, 4) + 3*pow(x, 3) - 15*pow(x, 2) - 2*x + 9;
}
double dFun(double x){
return 4*pow(x, 3) - 9*pow(x, 2) - 30*x - 2;
}

REPORT TITLE PAGE 9


c.
i.

REPORT TITLE PAGE 10


ii.

REPORT TITLE PAGE 11


iii.

REPORT TITLE PAGE 12


QUESTION-5:

a.

REPORT TITLE PAGE 13


b.
#include <iostream>
#include <cmath>
using namespace std;
double fun(double);
double gFun(double);
double iterMethod(double);
double ERR = 1e-6;
double PI = 3.14;
int main() {
double a = PI/2;
double solution = iterMethod(a);
cout << "solution: " << solution;
return 0;
}
double iterMethod(double a){
double x;
do {
x = gFun(a);
a = x;
}while(abs(fun(x)) > ERR);
return x;
}
double fun(double x) {
return 2*x - cos(x) - 3;
}
double gFun(double x) {
return (cos(x) + 3)/2;
}

REPORT TITLE PAGE 14

You might also like