0% found this document useful (0 votes)
7 views

cbnst code

Uploaded by

joshihimanshu358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

cbnst code

Uploaded by

joshihimanshu358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

12.

WAP to implement Euler's Method for solving ordinary


differential equations (ODEs).

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Define the differential equation dy/dx = f(x, y)


double f(double x, double y) {
return x + y; // Example: dy/dx = x + y
}

int main() {
double x0, y0, xn, h;
int n;

// Input initial values


cout << "Enter the initial value of x (x0): ";
cin >> x0;
cout << "Enter the initial value of y (y0): ";
cin >> y0;
cout << "Enter the value of x at which to find y (xn): ";
cin >> xn;
cout << "Enter the number of steps (n): ";
cin >> n;

// Step size
h = (xn - x0) / n;

// Euler's Method
double x = x0;
double y = y0;
cout << fixed << setprecision(6);
cout << "\nStep\tx\ty" << endl;

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


cout << i + 1 << "\t" << x << "\t" << y << endl;
y = y + h * f(x, y); // Euler's formula
x = x + h;
}

// Final value
cout << "\nThe value of y at x = " << xn << " is approximately " << y << endl;

return 0;
}

Output:
13. WAP to implements a simple linear curve fitting method using
the least squares approach.
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

// Function to calculate the sum of elements in a vector


double sum(const vector<double>& v) {
double total = 0;
for (double value : v) {
total += value;
}
return total;
}

// Function to calculate the sum of the product of corresponding elements of two vectors
double sum_of_products(const vector<double>& v1, const vector<double>& v2) {
double total = 0;
for (size_t i = 0; i < v1.size(); ++i) {
total += v1[i] * v2[i];
}
return total;
}

int main() {
int n;
cout << "Enter the number of data points: ";
cin >> n;

vector<double> x(n), y(n);

cout << "Enter the x values:\n";


for (int i = 0; i < n; ++i) {
cin >> x[i];
}

cout << "Enter the y values:\n";


for (int i = 0; i < n; ++i) {
cin >> y[i];
}

// Calculate required sums


double sum_x = sum(x);
double sum_y = sum(y);
double sum_x2 = sum_of_products(x, x);
double sum_xy = sum_of_products(x, y);

// Calculate coefficients a0 and a1 for y = a0 + a1 * x


double a1 = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
double a0 = (sum_y - a1 * sum_x) / n;

// Display the equation of the fitted curve


cout << fixed << setprecision(6);
cout << "The equation of the fitted curve is: y = " << a0 << " + " << a1 << " * x" << endl;

return 0;
}
Output:

You might also like