the logbook for numerical methods, Exercises of Numerical Methods in Engineering

This logbook shows the exercises i did for numerical methods and the code I used to complete these exercises.

Typology: Exercises

2020/2021

Available from 07/26/2022

samuel-higginbotham
samuel-higginbotham 🇬🇧

10 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Lincoln
Scientific computing
logbook semester A
MTH2008M
Samuel Higginbotham 16634112
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download the logbook for numerical methods and more Exercises Numerical Methods in Engineering in PDF only on Docsity!

University of Lincoln

Scientific computing

logbook semester A

MTH2008M

Samuel Higginbotham 16634112

Declaration

I confirm that all work in this logbook is my own and that all references from sources I used

have been acknowledged and will be shown.

  • Samuel Higginbotham Contents

// Calculate the percentage of difference diff=100*(dfn_for(h,x)-dfana(x))/dfana(x); cout <<f(x)<<"\n"; cout <<dfn_for(h,x)<<"\n"; cout <<dfana(x)<<"\n"; cout << "The value for the backwards method" << dfn_back(h, x) << endl; return 0; }

Task 1.

In this task I modified it by also adding in the symmetric difference also.

#include #include <math.h> using namespace std; double f(double x) {return(sin(x)); } double dfana(double x) {/* Analytical differential for comparison/return(cos(x)); } double dfn_for(double h,double x) {return(f(x+h)-f(x))/h; } double dfn_back(double h, double x) { return(f(x-h)-f(x))/h; } double dfn_cent(double h, double x) { return(f(x + h) - f(x - h)) / 2h; } int main() { double h=0.01; double x=1.2; double diff; // Calculate the percentage of difference diff=100*(dfn_for(h,x)-dfana(x))/dfana(x); cout <<f(x)<<"\n"; cout <<dfn_for(h,x)<<"\n"; cout <<dfana(x)<<"\n"; cout << "The value for the backwards method" << dfn_back(h, x) << endl; cout << "The value of the symmetric difference is :" << dfn_cent(h, x) << endl; return 0; }

Task 1. #include #include <math.h> #include using namespace std; double f(double x) { return(sin(x)); } double dfana(double x) {/* Analytical differential for comparison*/return(cos(x)); } double dfn_for(double h, double x) { return(f(x + h) - f(x)) / h; } double dfn_back(double h, double x) { return(f(x - h) - f(x)) / h; } double dfn_cent(double h, double x) { return(f(x + h) - f(x - h)) / 2 * h; } int main() { double h; double x = 1; double diff1, diff2, diff3; double hv[15] = { 0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005,0.0002,0.0001,0.00005,0. 00002,0.00001 }; cout << fixed << setprecision(5); cout << " ------------------------------------------------------------------" << endl; cout << " h symmetric forward backward" << endl; cout << " 3-points 2-point 2-point" << endl; cout << "--------------------------------------------------------------------" << endl; for (int i=0; i<15;i++) // this for loop will do the calculations for all the values of h { h = hv[i]; diff1 = ((dfn_cent(h, x) - dfana(x))); diff2 = ((dfn_for(h, x) - dfana(x))); diff3 = ((dfn_back(h, x) - dfana(x))); cout << setw(10) << h << setw(13) << diff1; cout << setw(16) << diff2; cout << setw(16) << diff3 << endl; } return 0; }

double hv[15] = { 0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005,0.0002,0.0001,0.00005,0. 00002,0.00001 }; cout << fixed << setprecision(5); cout << " ------------------------------------------------------------------" << endl; cout << " h symmetric forward backward 5 point" << endl; cout << " 3-points 2-point 2-point 5-point" << endl; cout << "--------------------------------------------------------------------" << endl; for (int i = 0; i < 15; i++) // this for loop will do the calculations for all the values of h { h = hv[i]; diff1 = ((dfn_cent(h, x) - dfana(x))); diff2 = ((dfn_for(h, x) - dfana(x))); diff3 = ((dfn_back(h, x) - dfana(x))); diff4 = ((point(h, x) - dfana(x))); cout << setw(10) << h << setw(13) << diff1; cout << setw(16) << diff2; cout << setw(16) << diff3 << endl; cout << setw(70) << diff4 << endl; } return 0; }

Task 2.

#include #include <math.h> #include using namespace std; double f(double x) { return(sin(x)); } double dfana(double x) {/* Analytical differential for comparison*/return(cos(x)); } double dfn_for(double h, double x) { return(f(x + h) - f(x)) / h; } double dfn_back(double h, double x) { return(f(x - h) - f(x)) / h; } double dfn_cent(double h, double x) { return(f(x + h) - f(x - h)) / 2 * h; } double point(double h, double x) { return((1 / 12) * h * (f(x - 2 * h) - 8 * f(x - h) + 8 * f(x + h) - f(x

  • 2 * h))); }

double point2(double h, double x) { return((1 / 12) * h * h * -(f(x - 2 * h) + 16 * f(x - h) + 16 * f(x + h)

  • f(x + 2 * h))); } double point3(double h, double x) { return((1 / 2) * h * h * h - (f(x - 2 * h) + 2 * f(x - h) - 2 * f(x + h)
  • f(x + 2 * h))); } double point4(double h, double x) { return((1 / h * h * h * h) * (f(x - 2 * h) - 4 * f(x - h) - 4 * f(x + h)
  • f(x + 2 * h))); } int main() { double h; double x = 1; double diff1, diff2, diff3, diff4, diff5, diff6, diff7; double hv[15] = { 0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005,0.0002,0.0001,0.00005,0. 00002,0.00001 }; cout << fixed << setprecision(5); cout << "

---------------------" << endl; cout << " h symmetric forward backward first second third fourth" << endl; cout << " 3-points 2-point 2-point 5-point 5-point 5-point 5-point" << endl; cout << "----------------------------------------------------------------------------- ----------------------" << endl; for (int i = 0; i < 15; i++) // this for loop will do the calculations for all the values of h { h = hv[i]; diff1 = ((dfn_cent(h, x) - dfana(x))); diff2 = ((dfn_for(h, x) - dfana(x))); diff3 = ((dfn_back(h, x) - dfana(x))); diff4 = ((point(h, x) - dfana(x))); diff5 = ((point2(h, x) - dfana(x))); diff6 = ((point3(h, x) - dfana(x))); diff7 = ((point4(h, x) - dfana(x))); cout << setw(10) << h << setw(13) << diff1; cout << setw(16) << diff2; cout << setw(16) << diff3 ; cout << setw(16) << diff4 ; cout << setw(16) << diff5 ; cout << setw(16) << diff6 ; cout << setw(16) << diff7 << endl; } return 0; } This code shows all of the different forms for calculating the derivative from a 2-point to a 5-point method.

double hv[15] = { 0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005,0.0002,0.0001,0.00005,0. 00002,0.00001 }; cout << fixed << setprecision(5); cout << "


---------------------" << endl; cout << " h symmetric forward backward first second third fourth" << endl; cout << " 3-points 2-point 2-point 5-point 5-point 5-point 5-point" << endl; cout << "----------------------------------------------------------------------------- ----------------------" << endl; for (int i = 0; i < 15; i++) // this for loop will do the calculations for all the values of h { h = hv[i]; diff1 = ((dfn_cent(h, x) - dfana(x))); diff2 = ((dfn_for(h, x) - dfana(x))); diff3 = ((dfn_back(h, x) - dfana(x))); diff4 = ((point(h, x) - dfana(x))); diff5 = ((point2(h, x) - dfana(x))); diff6 = ((point3(h, x) - dfana(x))); diff7 = ((point4(h, x) - dfana(x))); cout << setw(10) << h << setw(13) << diff1; cout << setw(16) << diff2; cout << setw(16) << diff3; cout << setw(16) << diff4; cout << setw(16) << diff5; cout << setw(16) << diff6; cout << setw(16) << diff7 << endl; } return 0; }

NOT FIN

Task 3.

#include #include #include <math.h> using namespace std; float func(float); // prototyping function func(x) float func(float x) // Function for the calculation of equation { return(pow(x, 3) - 1); // insert here the function to evaluate f(x) = (x^3-1) } int main() { char ans;

int kmax = 20; // Maximum number of iterations int k = 1; float x, a, b, eps; do { do { cin >> "input a"; cin >> "input b"; cin >> "input eps"; // add here the input of the values a,b,eps cout << "Confirm the inserted data (y/n) "; cin >> ans; } while (ans != 'y'); // // first bisection x = 0.5 * (a + b); cout << "\nCalculation of the root in the interval (" << a << "," << b << ")\n"; cout << "of the function f(x)=x^3-1 using the bisection method"; cout << "Convergence value (eps)= " << eps; cout << "Maximum number of iterations (kmax) = " << kmax << endl; cout << "Calculating the iteractions ... " << endl; cout << fixed << setprecision(4); cout << left << "N. cycle | x | f(x) | a | b |" << endl; // implement a while () loop that check both // the convergence variable (eps) // and the number of cycles (kmax) using a AND (&&) While( // ADD HERE THE MISSING PART ){ if (func(x) < 0) // assign the value a = x; else b = x; x = 0.5 * (a + b); //next bisection // Print iteration values in each cycle // add here the instruction to formatted print the row of the table // with the the value of k, x, func(x), a, b k++; // loop counter } // end of the while loop if (k > kmax) { // add here a statement that print a no convergence message // if the value of k is larger than kmax } cout << "Do you want to search another interval? "; cin >> ans; } while (ans != 'n'); return 0; }

using namespace std; double f(double x) { return (pow(x, 3) - 1); } double fp(double x) { return(3 * pow(x, 2)); } double newt(double x, double fx, double fx1) { return (x - (fx / fx1)); } int main() { double eps, x, x1, fx, fx1; cout.precision(6); //set the precision in the output writing cout << "Enter accuracy: \n"; //input the accuracy cin >> eps; //input an initial guess which can be any value cout << "Enter the initial guess: \n"; cin >> x1; cout << setprecision(6) << fixed; cout << " x(i)" << " " << " x(i+1)" << " " << " x(i+1)- x(i)" << endl; x = 0; while (fabs(x1 - x) >= eps) { x = x1; // Assign the variable x1 equal to x fx = f(x); // Calculate the value of the eq. in x fx1 = fp(x); //Calculate value of f’(x) x1 = newt(x,fx,fx1); // Newton iteration cout << x << " " << x1 << " " << abs(x1 - x) << endl; }; // continues the while-loop if the value is larger than eps cout << "The root of the equation is " << x1 << endl; return 0; } Task 4. F(x)=X^2 -

  • F(x)=0.125x^4+0.02x^3-0.02x^2-0.
  • Continuation of lab
  • F(x)= sinx^2/x^
  • F(x)=-1/x –log(x)+

Lab 5

The secant method

Task 5.

#include #include #include <math.h> #include using namespace std; double f(double x) { return (pow(x, 3) - 1); } double fp(double x) { return(3 * pow(x, 2)); } double newt(double x, double fx, double fx1) { return (x - (fx / fx1)); } double secant(double x, double fx, double x1) { return (x - fx * (x - x1) / (fx - f(x1))); } int main() { double eps, x, x1, fx, fx1,x2; cout.precision(6); //set the precision in the output writing cout << "Enter accuracy: \n"; //input the accuracy cin >> eps; //input an initial guess which can be any value cout << "Enter the initial guess: \n"; cin >> x1; cout << setprecision(6) << fixed; cout << " x(i)" << " " << " x(i+1)" << " x2(i+1)" << " " << " x(i+1)-x(i)" << endl; x = 0; while (fabs(x1 - x) >= eps) { x = x1; // Assign the variable x1 equal to x fx = f(x); // Calculate the value of the eq. in x fx1 = fp(x); //Calculate value of f’(x) x1 = newt(x, fx, fx1); // Newton iteration x2 = secant(x, x1, fx); cout << x << " " << x1 << " "<< x2 << " " << abs(x

  • x) << endl; }; // continues the while-loop if the value is larger than eps cout << "The root of the equation is " << x1 << endl; return 0; }

count1 = count1 + 1; x = x1; // Assign the variable x1 equal to x fx = f(x); // Calculate the value of the eq. in x fx1 = fp(x); //Calculate value of f’(x) x1 = newt(x, fx, fx1); // Newton iteration x2 = secant(x, x1, fx); cout << x << " " << x1 << " "<< x2 << " " << abs(x

  • x) << endl; }; // continues the while-loop if the value is larger than eps cout << "the number of cycles is:\n" << count1<<endl; cout << "The root of the equation is " << x1 << endl; return 0; }

Week 8 lab 6 (9/03/2020)

A recap on how pointers work

Task 1.

#include using namespace std; int main() { // Declare an int variable and assign an initial value int number = 88; // Declare a pointer variable pointing to an int (or int pointer) int* pNumber; // assign the address of the variable number to pointer pNumber pNumber = &number; // Print content of pNumber (-> machine dependent results) cout << pNumber << endl; // Print address of number (0x7fff57d7c9e8) cout << &number << endl; // Print value pointed to by pNumber (88) cout << *pNumber << endl; // Print value of number (88) cout << number << endl; // Re-assign value pointed to by pNumber *pNumber = 99; // Print content of pNumber (0x7fff57d7c9e8) cout << pNumber << endl; // Print address of number (0x7fff57d7c9e8) cout << &number << endl; // Print value pointed to by pNumber (99) cout << *pNumber << endl; // Print value of number (99) // The value of number changes via pointer cout << number << endl; // Print the address of pointer variable pNumber (0x7fff57d7c9e0) cout << &pNumber << endl; return 0; }

Example 2

#include using namespace std; int main () { int num; int *q; // Declare a pointer variable pointing to an int (or int pointer) q = # // assign the address of the variable number to pointer pNumber *q = 50; // assign the value 50 though q. cout << " q = " << q << ' ' << " num = " << num << ' ' << endl ; cout << " q = " << q << ' ' << " &num = " << &num << ' ' << endl ; (q)++; // increment num through q cout << " num = " << num << ' ' << endl ; (q)--; // decrease num through q cout << " num = " << num << ' ' << endl ; }