How to create an iterative structure? - c

I have created some code that has multiple functions, and when an X value is entered it outputs a line that has computed the number for the given X value and h value that has been declared. However, in the terminal it only outputs one line of each value, and I need to implement some type of iterative structure that varies h from 10^-1 to 10^-8. Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
double u(double x)
{
return(pow(x,3.0));
}
double dudx(double x)
{
return(3.0 * pow(x,2.0));
}
double fdiff(double x, double h)
{
return((u(x+h) - u(x))/h);
}
double bdiff(double x, double h)
{
return((u(x) - u(x-h))/h);
}
double cdiff(double x, double h)
{
return((u(x+h)-u(x-h))/(2*h));
}
double fderr(double x, double h)
{
return(fabs(fdiff(x,h)-dudx(x)));
}
double bderr(double x, double h)
{
return(fabs(bdiff(x,h)-dudx(x)));
}
double cderr(double x, double h)
{
return(fabs(cdiff(x,h)-dudx(x)));
}
double x;
double h=1.0 * pow(10,-1.0);
printf("Enter an Integer ");
scanf("%lf",&x);
printf("u(x): %lf du/dx: %lf fd: %lf bd: %lf cd: %lf fderr: %lf bderr: %lf cderr: %lf\n", u(x), dudx(x), fdiff(x,h), bdiff(x,h), cdiff(x,h), fderr(x,h), bderr(x,h), cderr(x,h));
}
I'm pretty sure I have to use some type of for loop around this area:
double h=1.0 * pow(10,-1.0);
But, I do not know how to implement it! So to reiterate, how do I create an iterative structure that starts with an h value of 10^-1 and ends with 10^-8?
Thank you in advance!

Related

How can I pass a function(which has its own parameters) as a parameter in a function?

I'm trying to pass the function that calculates the Values of a quadratic function, as a parameter to a function that calculates the surface under an Interval.
First I'm just trying to figure out how can I pass the parameters of the quadratic to it.
#include<stdlib.h>
#include<stdio.h>
double quadraticf(double x, double y, double z);
double interval_calculator(double(*quad_pnt)(double, double, double));
int main(){
interval_calculator(quadraticf);
return 0;
}
double interval_calculator(double (*quad_pnt)(double)){
quad_pnt(double f, double yg, double zh);
}
double quadraticf(double x, double y, double z){
printf("%lf, %lf, %lf \n",x,y,z);
}
You probably want something like this:
#include <stdlib.h>
#include <stdio.h>
double quadraticf(double x, double y, double z) {
printf("quadraticf: %lf, %lf, %lf \n", x, y, z);
return x + y + z;
}
double otherquadraticf(double x, double y, double z) {
printf("otherquadraticf: %lf, %lf, %lf \n", x, y, z);
return x * y * z;
}
double interval_calculator(double (*quad_pnt)(double, double, double), double f, double yg, double zh)
{
return quad_pnt(f, yg, zh);
}
int main() {
printf("result = %f\n", interval_calculator(quadraticf, 2.0, 3.0, 4.0));
printf("result = %f\n", interval_calculator(otherquadraticf, 2.0, 3.0, 4.0));
}

numerical integration using a pointer to a function always return 0

I am trying to use the function that I was given by my professor to calculate the integral of a polynomial function (polynomial such as: ax^2+bx+c). the function is:
double numbericalIntegration(double a ,double b ,double(*func)(double)){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x);
}
return sum*delta;
}
I changed a lot in order to integrate a polynomial function. but I was get the answer 0. why is that? and I'd appreciate if anybody tried to correct my work. my code is:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,3))/3 +(b*pow(x,2))/2 + (c*x);
}
double numbericalIntegration(double a ,double b ,double(*func)(double,double,double,double), double firstNum, double secondNum, double thirdNum){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x, firstNum, secondNum, thirdNum);
}
return sum*delta;
}
int main()
{
double (*func)(double,double,double,double);
func = integralPoly;
double sum = numbericalIntegration(2,4,func,1,1,4);
printf("sum = %d",sum);
return 0;
}
You need to change two things. First your polynomial function doesn't make any sense. You said it needs to be in the form of ax^2+bx+c but in your code polynomial is (ax^3)/3+(bx^2)/2+c*x. Your function should be:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,2)) +(b*x) + c;
}
Also you need to change your printf. %d is integer type specifier and you need double, so you need to use %f for example:
printf("sum = %f",sum);
Now the output of your program is:
sum = 32.666016
which is correct for your parameters.

C Program to Calculate Hypotenuse

I'm fairly new to coding and am currently learning C. In class I was given an assignment to write a program that calculates the hypotenuse of the triangle by using our own functions. However, there seems to be something wrong with the code that I have written.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y, double z);
int main(void) {
double side1, side2, side3, counter;
side3 = 1;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf_s("%d %d", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2, side3));
}
return 0;
}
double hypotenuse(double x, double y, double z) {
x *= x;
y *= y;
z = sqrt(x + y);
return z;
}
My instructor said that we're allowed to use the square root function sqrt of the math library. The main errors that I'm facing are:
1) side3 is not defined (This is why I just arbitrarily set it to 1, but is there some other way to prevent this error from happening?)
2) If I, for example, inputted 3 and 4 as side1 and side2, then side3 should be 5. However, the printed result is an absurdly long number.
Thank you for the help! Any words of advice are appreciated.
You don't need side3 variable - it is not used in calculation. And you function hypotenuse returns the result, so you can directly output the result of sqrt.
I use Ubuntu Linux and write it this way. Please look if you like it.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y) {
double z = sqrt(x * x + y * y);
return z;
}
int main(void) {
double b1, b2, counter;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &b1, &b2);
printf("%.2f\n", hypotenuse(b1, b2));
}
return 0;
}
Test
$ ./a.out
Enter values for two sides: 1 1.73
2.00
OP's code has some problems:
Key problem: Code should have generated a compiler warning as scanf() is directed to treat &side1 as an int *. Turn on all compiler warnings to save you time. Code used "%d" rather than the matching "%lf" to read a double. Also the return value should be checked to validate input.
double side1, side2, side3, counter;
...
// scanf_s("%d %d", &side1, &side2);
if (scanf_s("%lf %lf", &side1, &side2) != 2) puts("Input error");
size3 is not needed. Call hypotenuse() with 2 arguments. #ghostprgmr
// printf("%.2f\n", hypotenuse(side1, side2, side3));
printf("%.2f\n", hypotenuse(side1, side2));
// double hypotenuse(double x, double y, double z) {
double hypotenuse(double x, double y) {
double z = ...
Minor: Code used "%.2f" to print the value of the hypotenuse. This may be OK with select input values to OP's code, but is a poor choice, in general. If input values are vary small like 0.001 and 0.002, the output will print rounded value of 0.00. With very large values, the result will show many non-important digits as OP found with 130899030500194208680850288727868915862901750748094271410143‌​232.00.
For development and debugging, consider using "%e" ,"%g" or "%a" to see a relevant double.
Note that x * x + y * y is prone to over/under flow, even when mathematically sqrt(x * x + y * y) is in the double range. That is one advantage of the standard function hypot(x,y) as it usually handles those edge cases well.
As a reference for anyone viewing this question:
You don't need to write your own function. Standard C provides functions to calculate the hypotnuse:
7.12.7.3 The hypot functions
Synopsis
#include <math.h>
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);
Note that you likely need to link with -lm, though that's not listed explicitly in the function documentation in the C standard nor the latest POSIX documentation. It might be documented elsewhere in the standards.
(Link to C11 [draft] standard - likely to be much longer-lived.)
Use correct format specifiers!
Format Specifier for double is not %d! Rest is fine.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y, double z);
int main(void) {
double side1, side2, side3, counter;
side3 = 1;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2, side3));
}
return 0;
}
double hypotenuse(double x, double y, double z) {
x *= x;
y *= y;
z = sqrt(x + y);
return z;
}
Also you could modify it to this:
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y);
int main(void) {
double side1, side2, counter;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2));
}
return 0;
}
double hypotenuse(double x, double y) {
x *= x;
y *= y;
return sqrt(x + y);
}

Model using Euler method and pointer arithmetic not functioning

I'm new to C, and quite unfamiliar with writing any program larger than a few lines.
I'm trying to write a model for an object in freefall acted upon by gravity and drag. It uses Eulers method to solve two first order differential equations, one for position and one for velocity.
So we have: F = m dv/dt = -mg - k|v|v and dy/dt = v
These are solved by: Vn+1 = Vn - (delta t*(g+(k/m)|Vn|Vn)) and Yn+1 = Yn + (delta t * Vn)
(In this Vn+1 is the n+1th term etc.)
In my program i've tried to have two functions, for position and velocity, which work by passing pointers with Y and V values between them and the main function, and it should then loop until Y=0 and print off the values at each step.
When I run it it comes up with something like this: http://imgur.com/DNHIhHI
Could anyone tell me either what is wrong with this, or if I need to use a different approach completely?
Many Thanks, Code below
#include <stdio.h>
void Velocity(double *ptr, double m, double k, double t);
void Position(double *pst, double *ptr, double t );
int main()
{
double k = 18833.5608;
double t = 0;
double m;
double speed = 0;
double *ptr = &speed;
double y = 1000;
double *pst = &y;
printf("Enter mass of object: \n");
scanf("%f" , &m);
do
{
Velocity( ptr, m, k, t );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( pst, ptr, t);
printf("Position at time %f is: %f\n" , t , y);
t++;
}
while((y>0));
return 0;
}
void Velocity(double *velo, double m, double k, double t)
{
double g = 9.80665;
*velo = *velo - (t*(g+((k/m)*fabs(*velo)**(velo))));
}
void Position(double *Y , double *velo, double t )
{
*Y = *Y+(t*(*velo));
}
When writing programs that do calculations -- in any language, not just C -- try to make the code that does the computation take arguments and return results but not mutate variables. That is, do not write:
void do_calculation( double * result, double x, double y)
{
*result = x + y;
}
...
double r;
do_calculation(&r, 123, 456);
instead write
double do_calculation(double x, double y)
{
return x + y;
}
...
double r = do_calculation(123, 456);
Make sense?
If you want to modify an existing value, again, don't pass it in as a variable to be mutated. Instead of
void do_calculation(double * accumulator, double x, double y)
{
*accumulator = *accumulator + x + y;
}
...
double r = 10;
do_calculation(&r, 123, 456);
instead say
double do_calculation(double original, double x, double y)
{
return original + x + y;
}
...
double r = 10;
r = do_calculation(r, 123, 456);
Now, once you've got your program architected more sensibly, you need to learn how to debug small programs. Some good advice on that subject can be found here:
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/
A misconcept. I believe you're trying to solve the equations by using small increments of time. Nothing wrong with that, just make the time increment as small as possible, and correct the formulas:
#include <stdio.h>
#include <math.h>
void Velocity(double *velocity, double m, double k, double t)
{
double g = 9.80665;
double velo = *(velocity);
velo = velo - (t*(g+((k/m)*abs(velo)*(velo))));
*(velocity)=velo;
}
void Position(double *position , double *velocity, double t )
{
double Y = *(position);
double velo = *(velocity);
Y = Y+(t*(velo));
*(position)=Y;
}
int main()
{
double k = 18833.5608;
double t = 0;
double dt = 0.001; //making a small increment of time
double m=100;
double speed = 0;
double y = 1000;
//printf("Enter mass of object: \n");
//scanf("%f" , &m);
do
{
Velocity( &speed, m, k, dt );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( &y, &speed, dt);
printf("Position at time %f is: %f\n" , t , y);
t+=dt; //increment time by delta t
}
while((y>0));
return 0;
}

Errors about too few arguments

The below returns too few arguments in the int main section on the fp and newton lines.
Can you explain this?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double a, double b, double c, double x)
{
return pow(x,3)+a*pow(x,2)+b*x+c;
}
double fp(double a, double b, double c, double x)
{
return 3*pow(x,2)+(2*a*x)+b+0*c;
}
double newton(double a, double b, double c, double x)
{
return x - (f(a,b,c,x)/fp(a,b,c,x));
}
int main()
{
double a,b,c,x1,x2;
int i;
char *input = getenv("QUERY_STRING");
sscanf(input, "coeff1=%lf &coeff2=%lf &coeff3=%lf &x=%lf", &a, &b, &c, &x1);
if (fp(x1)==0.0)
{
printf("No convergence.\n");
return 0;
}
for (i=0;i<100;i++)
{
x2=newton(x1);
if (fabs(x1-x2)<= pow(10,-10)) break;
x1=x2;
}
printf("iteration = %d\n", i);
printf("x= %lf\n", x1);
return 0;
}
Just what the error says! You haven't passed enough arguments:
This prototype:
fp(double a, double b, double c, double x) {
means you need to pass four arguments, like:
fp(x1, what, about, these);
The same goes for newton.
Also, regarding if (fp(x1)==0.0) - While floating point zero values can be compared with each other (zero is excatly zero), remember that floating-point on computers is not exact. For that reason, you should always compare against some epsilon value:
#define EPSILON 0.0001 // salt to taste
#define ABS(x) ( ((x)<0) ? -(x) : x )
#define FP_EQUALS(x,y) (ABS(x-y) < EPSILON)
//if (d == 0.0) {
if (FP_EQUALS(d, 0.0)) {
// d is "zero"
}
//if (a == b) {
if (FP_EQUALS(a, b) {

Resources