This lab has two parts
Go through your C files and remove any
The header file, ideally, should have the same name as that of
the library you want to create. I want my library to be
called
Insert the line
The last step in creating the library is to combine all
the
Modify the code below (in Cell 3) for finding roots of an equation with
your own functions. Copy it into a
file
If it gives the correct answer, you are done.
You can also get an error saying that the library is not found. In that case, do the following
/************************************************************************
* This is the header file for the functions in the cbnm library.
* The library comprises functions at various levels
* 1. Basic Matrices and Vectors
* 2. Matrix Operations
* 3. Root Finding
* 4. Systems of Equations
* 5. Regression
* 6. Interpolation
* 7. Integration
* 8. Ordinary Differential Equations
* - Author: Chakravarthy Bhagvati (2023)
************************************************************************/
#define ROWS(M) (M->nrows)
#define COLS(M) (M->ncols)
typedef struct {
double *val;
int length;
} VECTOR;
typedef struct {
double **array;
int nrows;
int ncols;
} MATRIX;
typedef double DFUNC(double); /* This defines functions for
root finding code */
/************************************************************************
* The following are the Level 1 functions: matrix and vector basics
************************************************************************/
MATRIX *new_matrix(int, int, double);
MATRIX *mat_identity(int);
MATRIX *mat_transpose(MATRIX *);
MATRIX *new_random_matrix(int, int, ...);
MATRIX *mat_copy(MATRIX *);
MATRIX *vector_to_matrix(VECTOR *);
MATRIX *mat_set(MATRIX *, ...);
MATRIX *mat_get_slice(MATRIX *, int, int, int, int);
MATRIX *mat_concat_col(MATRIX *, VECTOR *);
VECTOR *mat_get_diagonal(MATRIX *);
VECTOR *new_vector(int, double);
VECTOR *vec_set(VECTOR *, ...);
VECTOR *mat_get_row(MATRIX *, int);
VECTOR *mat_get_column(MATRIX *, int);
VECTOR *vec_vrange(int, int, double);
double mat_get_value(MATRIX *, int, int);
double mat_set_value(MATRIX *, int, int, double);
void mat_print(MATRIX *);
void vec_print(VECTOR *);
/************************************************************************/
MATRIX *mat_add_scalar(MATRIX *, double);
MATRIX *mat_divide_scalar(MATRIX *, double);
MATRIX *mat_mult_scalar(MATRIX *, double);
MATRIX *mult_pairwise(MATRIX *, MATRIX *);
MATRIX *mat_mult(MATRIX *, MATRIX *);
VECTOR *vec_index(MATRIX *, double);
double mat_getmax(MATRIX *);
double mat_getmin(MATRIX *);
double mat_getmean(MATRIX *);
int vec_argmax(VECTOR *, int, int);
/************************************************************************/
double root_bisection(DFUNC *func, float low, float high,
double eps, int maxiter);
double root_secant(DFUNC *func, float init,
double eps, int maxiter);
/************************************************************************/
MATRIX *mat_vandermonde(VECTOR *, int);
MATRIX *cbnm_gauss_eliminate(MATRIX *);
MATRIX *cbnm_back_substitute(MATRIX *);
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cbnm2023.h"
double *fifth_root100(double x)
{
double x5 = pow(x, 5.0);
return(x5 - 100.0);
}
int main(void)
{
double ans;
ans = root_bisection(fifth_root100, 1.0, 10.0, 0.000001, 50);
printf("The fifth root of 100.0 is %lf\n", ans);
printf("Verification:\n\t");
printf("Calculated answer**5 = %lf\n", pow(ans, 5.0));
exit(0);
}