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

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...
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...
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
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...
(***ONLY respond if you can address all questions. If not, please refrain from answering and let...
(***ONLY respond if you can address all questions. If not, please refrain from answering and let the next kind available expert address these conceptual questions. Thank you in advance). 1.) The difference between the total factory overhead cost in the flexible budget for the actual units produced and the amount of factory overhead cost applied to products using the standard overhead rate is called the factory overhead ______________: Multiple Choice: (A) Flexible-budget variance (B) Production-volume variance (C.) Total fixed cost...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
c) Let R be any ring and let ??(?) be the set of all n by...
c) Let R be any ring and let ??(?) be the set of all n by n matrices. Show that ??(?) is a ring with identity under standard rules for adding and multiplying matrices. Under what conditions is ??(?) commutative?
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT