In: Computer Science
write me a java progrm that does this:
y=x^3 +1
drivative of y=3X^2+0
y=mx+b
m is slope for x=2 put 2 back to equation y=3(2)^2=12
now we have 12 and y=9 so
9=2(12)+b and we find b=-15
now we write new equation that with these information
y=12x-15 we solve for 12x-15=0, we solve for x and x=15/12=1.25
now we do the same thing above we put at the drivative equation y=3(1.25)^2+0=4.6875
now we put this to 2.943125=(4.6875)*3+b
we solve for b=-2.90625
y=4.6875x-2.90625 and 4.6875x-2.90625=0 we solve for x and =0.62 now we use this as we did above to find another new equation and this go in a loop to do the same thing over and over to find new point and equation
/*----------------------------------------------------\
|save code 'equation.java' |
|compile using 'javac equation.java'
|
|run using 'java equation' |
\----------------------------------------------------*/
import java.lang.Math;
//your file name class name must be same here file name is
'equation.java'
class equation{
public static void main(String args[]){
float x = 2f;//initial value of
x which is given
int i = 6;//loop iterator
for(i=0; i<6; i++){//calling in
loop over and over again
x =
printEquation(x);
}
}
// Function to find values and equation
public static float printEquation(float x){
float y1, m, b, output;
y1 = (float) Math.pow(x, 3) +
1;//given function---(1)
m = (float) Math.pow(x, 2) *
3;//darivative of function(1) slope
b = (float) y1 - m*x;//intersection
part of equation
output = (float) (-1)*b / m;//next
x value
System.out.println("\nFor value x =
"+x);
System.out.println("Equation will
be y = ("+m+")*x + ("+b+")\n");
return output;
}
}
//screen shots