Question

In: Computer Science

Can you please code in C the Lagrangian function. If you have any questions please let...

Can you please code in C the Lagrangian function.

If you have any questions please let me know.

Solutions

Expert Solution

#include <stdio.h>

// Defines a structure to store x and y = f(x)
struct Lagrangian
{
float x, y;
};// End of structure

// Function to to implement Lagrange's formula
// newPoint having new data point whose value needs to be extracted
// numPoint represents the number of points
float lagrangianFunction(struct Lagrangian f[], float newPoint, int numPoint)
{
float filanResult = 0;
// Loop variable
int c, d;
// Loops till number of points
for(c = 0; c < numPoint; c++)
{
// Compute each term result
float termValue = f[c].y;

// Loops till number of points to calculate termValue
for(d = 0; d < numPoint; d++)
// Checks if not diagonal
if (d != c)
// Calculates the term value based on the formula
termValue *= (newPoint - f[d].x) / (float)(f[c].x - f[d].x);

// Adds current term result to final result
filanResult += termValue;
}// End of for loop
// Returns the result
return filanResult;
}// End of function

// main function definition
int main()
{
float xValue;
int numPoint;
int c;

// Accepts the size
printf("\n Enter the number of the terms: ");
scanf("%d", &numPoint);

// Declares an array of object of size numPoint
struct Lagrangian f[numPoint];


printf("\n\n Enter values for variables x and y: \n");
// Loops till numPoint to accept x and y value
for(c = 0; c < numPoint; c++)
scanf("%f %f",&f[c].x, &f[c].y);

printf("\n\n Entered Values:\n");
for(c = 0; c < numPoint; c++)
printf(" %4.2f \t %4.2f \n", f[c].x, f[c].y);


printf(" \n\n Enter the value of the x to find the respective value of y: ");
scanf("%f", &xValue);

// Calls the function to calculate and displays the return result
printf("\n Value of f(%4.2f) is : %f", xValue, lagrangianFunction(f, xValue, numPoint));
return 0;
}// End of main function

Sample Output:

Enter the number of the terms: 4


Enter values for variables x and y:
5 140
13 256
17 1125
23 2533


Entered Values:
5.00 140.00
13.00 256.00
17.00 1125.00
23.00 2533.00


Enter the value of the x to find the respective value of y: 12

Value of f(12.00) is : 93.762733


Related Solutions

Please answer in C++! Let me know if you have any questions about these. Program 1:...
Please answer in C++! Let me know if you have any questions about these. Program 1: For this program, imagine we want to track an object and detect if it goes off the left or right side of the screen (that is, it’s X position is less than 0 and greater than the width of the screen, say, 100). Write a program that asks the user for the starting X and Y position of the object as well as the...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
INTERVIEW QUESTIONS Please describe any retail or cashing experience you have Please describe any customer services...
INTERVIEW QUESTIONS Please describe any retail or cashing experience you have Please describe any customer services experience you have
Original C code please. Part 1: You can do A, B, and C in one program...
Original C code please. Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7. 14        28       ...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Let A ⊆ R, let f : A → R be a function, and let c...
Let A ⊆ R, let f : A → R be a function, and let c be a limit point of A. Suppose that a student copied down the following definition of the limit of f at c: “we say that limx→c f(x) = L provided that, for all ε > 0, there exists a δ ≥ 0 such that if 0 < |x − c| < δ and x ∈ A, then |f(x) − L| < ε”. What was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT