# Thread: Linear equations

1. ## Linear equations

Has anyone wrote a program solve this f(x) = sin (2x/5) -x +1 =0
with bisection method and newtons method?

I haven't a clue and any help would be appreciated.

Thanks
Amanda

double left = 0 ,right = 0;
double size, mid,root, tol = .00001, zero_check = .0001;
int done = 0;
float finish;

printf("Please enter a range of values ");
scanf ("%lf ", &left);
scanf ("%lf", &right);

size = right - left;

while((size >=tol) && (!done))
{
mid = (right+left)/2;
printf("f(%f) = %9.6f f(%f) = %9.6f f(%f) = %9.6f\n",
left, f(left),mid,f(mid),right,f(right));

if(fabs(f(mid)) <zero_check)
done = 1;
else if (f(mid) * f(left)<0)
right = mid;
else
left = mid;
size = right - left;
}

if(size >tol)
root = mid;
else
root = (left +right)/2;
printf("The root is %lf\n", root);

}
double f(double x)
{
return(x*x -4 +exp(-1.0*x));
}

I want to know why do you use the function double f. Can you not just check the values without applying them to this function?And when you take the right from the left what is that value called and when do you compare it the tol?

2. I've written a few solver routines, using both methods. But I'm not sure what the problem is... you don't know either of those methods is? You don't know how to take the derivative for Newton's method? Try posting what you've written so far, even if it's pseudocode. (Just use code tags when doing so..)

3. ## Linear equations

Sorry about this i dont really know how to use this message board so I am going to write the next part of my question in here.
Code:
```double left = 0 ,right = 0;
double size, mid,root, tol = .00001, zero_check = .0001;
int done = 0;
float finish;

printf("Please enter a range of values ");
scanf ("%lf ", &left);
scanf ("%lf", &right);

size = right - left;

while((size >=tol) && (!done))
{
mid = (right+left)/2;
printf("f(%f) = %9.6f f(%f) = %9.6f f(%f) = %9.6f\n",
left, f(left),mid,f(mid),right,f(right));

if(fabs(f(mid)) <zero_check)
done = 1;
else if (f(mid) * f(left)<0)
right = mid;
else
left = mid;
size = right - left;
}

if(size >tol)
root = mid;
else
root = (left +right)/2;
printf("The root is %lf\n", root);

}
double f(double x)
{
return(x*x -4 +exp(-1.0*x));
}```
I want to know why do you use the function double f. Can you not just check the values without applying them to this function?And when you take the right from the left what is that value called and when do you compare it the tol?

T

&#91;code]&#91;/code]tagged by Salem

4. ## Linear equations

I want to know though why do you use these values in the function. Why subtitute your values into the function. What does that do to your values and is it nessecary to do this. Is that function f a generic function that is used with all linear equation problems? Can you not just use your values raw and not put them through the function?

Thanks
Amanda

5. What Salem said, it's a lot easier to use a function than to "use the values raw" as you put it.

Code:
```double f(double x){
return(sin(2*x/5)-x+1);
}```
Or whatever the function is you're trying to solve. I should also make some other points:
• Bisection only works if the function changes sign over the input interval. Check that f(upper)*f(lower) < 0 after inputting the interval bounds.
• the variable "finish" is never used.
• that the routine does not allow roots < 0. Try:
size = fabs(right-left);
• that neither this equation nor the one in the other program are linear equations.

6. ## Re: Linear equations

Originally posted by amandad40
I want to know though why do you use these values in the function. Why subtitute your values into the function. What does that do to your values and is it nessecary to do this. Is that function f a generic function that is used with all linear equation problems? Can you not just use your values raw and not put them through the function?

Thanks
Amanda
It is a lot easier to have that function (equation) in one place, especially if you use it in several places.
Otherwise, you have to modify your code in several places if you want to change the equation.

It's also a better coding style to not print the same code in several places.

Popular pages Recent additions