





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This logbook shows the exercises i did for numerical methods and the code I used to complete these exercises.
Typology: Exercises
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















// 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; }
#include
Task 1. #include
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; }
#include
double point2(double h, double x) { return((1 / 12) * h * h * -(f(x - 2 * h) + 16 * f(x - h) + 16 * f(x + h)
---------------------" << 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; }
#include
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 -
#include
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
#include
#include