c++ bisection problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;

int main()
 {
	double h,v,w,z,y;
	
	cout << "Enter the width, W: ";
	cin >> w;
	
		if (w<0)
		{
			cout << "Error: The Width must be strictly positive." << endl;
			return EXIT_FAILURE;
		}
	
	cout << "Enter the height, H: ";
	cin >> h;
	
		if (h<0)
		{
			cout << "Error: The Height must be strictly positive." << endl;
			return EXIT_FAILURE;
		}
	
	
	x=(6*x*x)+(x(-2h-2w))+((h*w)/2); // is this my x?
	
	y=((w/2)-x);
	z=(h-2x);
	v=(2*x*x*x)-(h*x*x)-(w*x*x)+((h*w)/2)
	
	
	return EXIT_SUCCESS;
 }

That's all I got so far we have to do this:
[spoiler]Suppose you want to build a covered box by cutting sections out of a W by H sheet of
cardboard (in meters) and folding as shown in the figure below (cutout the shaded regions
and fold on the dotted lines).

https://dl.dropbox.com/u/28097097/image.jpg


Derive an expression for the volume, V , of the box and analytically compute its derivative
as a function of x.

Write a program to find the roots of your derivative and so find the dimension for x (and
by extension y, and z) that maximizes the volume. Your program should read the values
of W and H from standard input after providing an appropriate prompt, find the maximal
volume, then print the maximal volume and the maximizing dimensions x, y, and z. Your
program should automatically determine the allowed range of values for the dimensions given
W and H (note some care is needed when they are equal). Use a constant tolerance of 0:01
meters for the convergence criteria in the bisection.


[/spoiler] I've already found the derivative, which is in my code, but I really don't understand bisection that much and how I get the ranges using bisection . I can do the rest after that point, but the bisection just has me stuck.

Basically I'm lost in what am I suppose to set equal to x, so I could get the rest of the equations to work.
Last edited on
I think I got it, but I'm not sure... The only problems I can see is if the compiler get's confused with what is what for my while and if statements.
I also set the functions bounds to be constant.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;

int main()
 {
	double a,b,c,h,v,w,x,y,z, tolerance;
	tolerance=0.01;
	
	cout << "Enter the width, W: ";
	cin >> w;
	
		if (w<0)
		{
			cout << "Error: The Width must be strictly positive." << endl;
			return EXIT_FAILURE;
		}
	
	cout << "Enter the height, H: ";
	cin >> h;
	
		if (h<0)
		{
			cout << "Error: The Height must be strictly positive." << endl;
			return EXIT_FAILURE;
		}
	
	a=0;
	b=(w/2);
	c=((a+b)/2);
	
		while (abs(a-b)>=tolerance)
		{
			if ((w>=0) && (h>=0) && (a>=0))
			{
				if ((w<=0) && (h<=0) && (c<=0))
				{
				b=c;
				}
			else 
			 {
				if ((w>=0) && (h>=0) && (c>=0))
				if ((w<=0) && (h<=0) && (b<=0))
				a=c;
				c=((a+b)/2);
			 }
			}
		break;
		}
	x=c;
	
	// x=(6*x*x)+(x(-2h-2w))+((h*w)/2);
	y=((w/2)-x);
	z=(h-(2*x));
	v=(2*x*x*x)-(h*x*x)-(w*x*x)+((h*w)/2)
	
	cout << "x= " << x << endl;
	cout << "y= " << y << endl;
	cout << "z= " << z << endl;
	cout << "v= " << v << endl;

	return EXIT_SUCCESS;
 }


I can't test it because when I try to compile I get this error
"line(59) : error C2146: syntax error : missing ';' before identifier 'cout'


Also what would I need to make it look "cleaner"?
Last edited on
L58 needs to end with a ;
Sorry no idea what you are trying to do
Topic archived. No new replies allowed.