Cpsc 440 Problem Set 2 Question 1

# Cpsc 440 Problem Set 2 Question 1 - hw2p1.h ifndef HW2P1_H...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hw2p1.h 2/25/2008 # ifndef HW2P1_H # define HW2P1_H typedef struct lagrange_polynomial * lagrange_polynomial ; lagrange_polynomial newLagrangePolynomial ( void ) ; void freeLagrangePolynomial ( lagrange_polynomial lp ) ; double evaluateLagrangePolynomial ( lagrange_polynomial lp , double x ) ; void getEquispacedNodes ( double min , double max , int count , double * nodes ) ; void getChebyshevNodes ( double min , double max , int count , double * nodes ) ; double f1 ( double x ) ; double f2 ( double x ) ; # endif 1 hw2p1.c 4/1/2008 # include < math . h > # include < stdio . h > # include < stdlib . h > # include "hw2p1.h" # define PI 3.141592653589793 # define MIN_VECTOR_SIZE 10 # define N_MIN 2 # define N_MAX 40 # define X_MIN- 1 # define X_MAX 1 struct lagrange_polynomial { int num_data ; int capacity ; double * x_values ; double * y_values ; } ; lagrange_polynomial newLagrangePolynomial ( void ) { lagrange_polynomial lp = malloc ( sizeof ( struct lagrange_polynomial ) ) ; lp- > num_data = ; lp- > capacity = MIN_VECTOR_SIZE ; lp- > x_values = malloc ( lp- > capacity * sizeof ( double ) ) ; lp- > y_values = malloc ( lp- > capacity * sizeof ( double ) ) ; return ( lp ) ; } void freeLagrangePolynomial ( lagrange_polynomial lp ) { free ( lp- > x_values ) ; free ( lp- > y_values ) ; free ( lp ) ; } void addDataPoint ( lagrange_polynomial lp , double x_value , double y_value ) { if ( lp- > num_data = = lp- > capacity ) { 1 hw2p1.c 4/1/2008 lp- > capacity * = 2 ; lp- > x_values = realloc ( lp- > x_values , lp- > capacity * sizeof ( double ) ) ; lp- > y_values = realloc ( lp- > y_values , lp- > capacity * sizeof ( double ) ) ; } lp- > x_values [ lp- > num_data ] = x_value ; lp- > y_values [ lp- > num_data ] = y_value ; lp- > num_data + + ; } double evaluateLagrangePolynomial ( lagrange_polynomial lp , double x ) { double lp_result = ; for ( int j = ; j < lp- > num_data ; j + + ) { double cur_term = 1 ; for ( int i = ; i < lp- > num_data ; i + + ) { if ( i ! = j ) { double xi = lp- > x_values [ i ] ; double xj = lp- > x_values [ j ] ; cur_term * = ( x- xi ) / ( xj- xi ) ; } } lp_result + = lp- > y_values [ j ] * cur_term ; } return ( lp_result ) ; } int main ( int argc , char * argv ) { // Extract the command-line...
View Full Document

## This note was uploaded on 07/19/2008 for the course CPSC 440 taught by Professor Vladimirrokhlin during the Spring '08 term at Yale.

### Page1 / 8

Cpsc 440 Problem Set 2 Question 1 - hw2p1.h ifndef HW2P1_H...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online