# fafdsfgrefdhtrh 5tehtreterqr, Formulas and forms for Trade Union Law. Università degli Studi di Bari Aldo Moro

DOCX (278 KB)
4 pages
21Number of visits
Description
ERRETRGFDG RAGAT Ffafwet gdrgtdh jnyhregfe
20 points
this document
Preview3 pages / 4

Problem 1

Coding:

#include "stdafx.h" #include<stdio.h>

float f(float x){ return(x / (1 + x * x));

} int main(){

int n; float a, b, h, I, sum = 0, x; printf("Enter the lower limit a: "); scanf_s("%f", &a); printf("Enter the upper limit b: "); scanf_s("%f", &b); printf("\nEnter the number of subintervals n: "); scanf_s("%d", &n);

h = (b - a) / n;

int i; for (i = 1; i < n; i++) {

x = a + i * h; if (i % 2 == 0) {

sum = sum + 2 * f(x); } else {

sum = sum + 4 * f(x); }

} I = h / 3 * (f(a) + sum + f(b)); printf("\nThe value of integral is: %f", I);

}

Result:

Conclusion:

The results in the C++ program show the true value of the integral is 0.346574 which is the same as in hand calculations. As we can see, as the number of subintervals increases, the value gets closer to the true value.

Problem 2

Coding:

#include "stdafx.h" #include <stdio.h> #include <conio.h> #include <math.h> void GaussLegendre(float, float, int);

float f(float x) { return(0.5*(0.5*(x + 1)*0.5*(x + 1) * exp(-(0.5*(x + 1)*0.5*(x + 1)))));

} float g(float a, float b, float z) {

float x = (b - a) / 2 * z + (b + a) / 2; return (x*x * exp(-(x*x)));

} int main() {

int n; float a, b; printf("Enter the lower limit a: "); scanf_s("%f", &a); printf("Enter the upper limit b: "); scanf_s("%f", &b); printf("Enter value of n for n-points formula: "); scanf_s("%d", &n); switch (n) { case 2:

printf("Using 2-point Formula:\n"); GaussLegendre(a, b, n); break;

case 3: printf("Using 3-point Formula:\n"); GaussLegendre(a, b, n); break;

case 4: printf("Using 4-point Formula:\n"); GaussLegendre(a, b, n); break;

case 5: printf("Using 5-point Formula:\n"); GaussLegendre(a, b, n); break;

} _getch();

}

void GaussLegendre(float a, float b, int n) { float I; if (a == -1 && b == 1) {

if (n == 2) {

I = 1 * f(1 / sqrt(3)) + 1 * f(-1 / sqrt(3)); printf("I=%f", I);

} if (n == 3) {

I = (5 * (f(sqrt(0.6)) + f(-sqrt(0.6))) + 8 * f(0))/9; printf("I=%f", I);

} if (n == 4) {

I = 0.34785*(f(0.86114) + f(-0.86114)) + 0.65215*(f(0.33998) + f (-0.33998));

printf("I=%f", I); } if (n == 5) {

I = 0.23693*(f(0.90618) + f(-0.90618)) + 0.47863*(f(0.53847) + f (-0.53847)) + 0.56889*(f(0));

printf("I=%f", I); }

} else {

if (n == 2) {

I = (b - a) / 2 * (1 * (g(a, b, 1 / sqrt(3)) + g(a, b, -1 / sqrt(3)))); printf("I=%f", I);

} if (n == 3) {

I = (b - a) / 2 * ((5 * (g(a, b, sqrt(0.6)) + g(a, b, -sqrt(0.6)))) + (8 * g(a, b, 0)))/9;

printf("I=%f", I); } if (n == 4) {

I = (b - a) / 2 * (0.34785 * (g(a, b, 0.86114) + g(a, b, -0.86114)) + 0.65215 * (g(a, b, 0.33998) + g(a, b, -0.33998)));

printf("I=%f", I); } if (n == 5) {

I = (b - a) / 2 * (0.23693 * (g(a, b, 0.90618) + g(a, b, -0.90618)) + 0.47863 * (g(a, b, 0.53847) + g(a, b, -0.53847)) + 0.56889*g(a, b, 0));

printf("I=%f", I); }

} }

Result:

Conclusion:

The results in the C++ program are the same as in hand calculations. The results on the left are integrated with limit from 0 to 1 and the results on the right are integrated with limit from –1 to 1 with both resulting in the same value. As we can see, as the value of n-points increases, the value of the integral gets closer to the true value.