Code:
phy-pc3195:Desktop guest$ gcc Newton2.c -o Newton
Newton2.c: In function ‘main’:
Newton2.c:30: error: incompatible type for argument 3 of ‘f’
Newton2.c:35: error: incompatible type for argument 3 of ‘f’
Newton2.c: In function ‘newton’:
Newton2.c:53: error: incompatible type for argument 3 of ‘f’
Newton2.c:53: error: incompatible type for argument 3 of ‘f_prime’
Here's my code:
#include <stdio.h>
#include <math.h>
double newton(double x_0, double tol, int max_iters,
int* iters_p, int i, int* converged_p);
double f(double n, int i, double x[i]);
double f_prime(double n, int i, double x[i]);
int main() {
int i; /* One of four temperature ranges*/
double x_0; /* Initial guess */
double x[3]; /* Approximate solution */
double tol; /* Maximum error */
int max_iters; /* Maximum number of iterations */
int iters; /* Actual number of iterations */
int converged; /* Whether iteration converged */
float n; /* Number density */
printf("Enter x_0, tol, and max_iters\n");
scanf("%lf %lf %d", &x_0, &tol, &max_iters);
for(n=0.01;n<=100;n=n*1.05) {
for(i=0;i<4;i++) {
x[i] = newton(x_0, tol, max_iters, &iters, i, &converged);
if (converged) {
printf("Newton algorithm converged after %d steps.\n",
iters);
printf("The approximate solution is %19.16e\n", x[i]);
printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x[i]));
} else {
printf("Newton algorithm didn't converge after %d steps.\n",
iters);
printf("The final estimate was %19.16e\n", x[i]);
printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x[i]));
}
}}
return 0;
} /* main */
double newton(double x_0, double tol, int max_iters,
int* iters_p, int i, int* converged_p) {
double x;
double x_prev;
int iter = 0;
float n;
x=x_0;
do {
iter++;
x_prev = x;
x = x_prev - f(n, i, x_prev)/f_prime(n, i, x_prev);
} while (fabs(x - x_prev) > tol && iter < max_iters);
if (fabs(x - x_prev) <= tol)
*converged_p = 1;
else
*converged_p = 0;
*iters_p = iter;
return x;
} /* newton algorithm */
double f(double n, int i, double x[i]) {
if (i=0) {
return 1-((3.42e+16*pow(x[i],2.12)*n)/0.015);
}
else if (i=1) {
return 1-((9.10e+18*x[i]*n)/0.015);
}
else if (i=2) {
return 1-((1.11e+20*pow(x[i],0.56)*n)/0.015);
}
else if (i=3) {
return 1-((2.00e+8*pow(x[i],3.67)*n)/0.015);
}
else printf("Temperature outside the given range");
} /* f */
double f_prime(double n,int i, double x[i]) {
if (i=0) {
return (-7.2504e+16*pow(x[i],1.12)*n)/0.015;
}
else if (i=1) {
return (-9.10e+18*n)/0.015;
}
else if (i=2) {
return (-6.216e+19*pow(x[i],-0.44)*n)/0.015;
}
else if (i=3) {
return (-7.34e+8*pow(x[i],2.67)*n)/0.015;
}
else printf("Temperature outside given range");
} /* f_prime */
Any help would be appreciated, thanks a lot.