15,905,683 members
1.00/5 (2 votes)
See more:
Write one program (in java language) to calculate the following:
A-Root finding by the next methods:
```1. Drawing.
2. Fixed point iteration.
3. Bisection.
4. Newton Raphson.
5. False position.
B-Interpolation and extrapolation by the next methods:
a. Newton forward.
b. Newton backward.
c. Lagrange method.
d. Splines.```

C-The program should continue running and execute the selected
methods until the user chooses to exit the program.
Every method should represent by one or a function section, and all
method functions should be called by a main function.

What I have tried:

I did not know how except
```import java.util.Scanner;

public class RootInterpExtrap {

// Function to perform false position root finding
public static double falsePosition(double f(double), double a, double b, double tol, int maxIter) {
double fa = f(a);
double fb = f(b);
double c = a;
for (int i = 0; i < maxIter; i++) {
c = (a * fb - b * fa) / (fb - fa);
double fc = f(c);
if (fc == 0 || (b-a)/2 < tol) {
break;
}
if (fc * fb < 0) {
fa = fb;
a = b;
} else {
fa = fc;
}
b = c;
fb = fc;
}
return c;
}

// Function to perform spline interpolation and extrapolation
public static double spline(double[] x, double[] y, double x0) {
int n = x.length;
double[] h = new double[n-1];
double[] b = new double[n-1];
double[] u = new double[n-1];
double[] v = new double[n-1];
for (int i = 0; i < n-1; i++) {
h[i] = x[i+1] - x[i];
b[i] = (y[i+1] - y[i]) / h[i];
}
u[1] = 2 * (h[0] + h[1]);
v[1] = 6 * (b[1] - b[0]);
for (int i = 2; i < n-1; i++) {
u[i] = 2 * (h[i-1] + h[i]) - (h[i-1]*h[i-1]) / u[i-1];
v[i] = 6 * (b[i] - b[i-1]) - (h[i-1]*v[i-1]) / u[i-1];
}
double[] z = new double[n];
for (int i = n-2; i > 0; i--) {
z[i] = (v[i] - h[i]*z[i+1]) / u[i];
}
z[0] = 0;
z[n-1] = 0;
int j = 0;
for (int i = 1; i < n; i++) {
if (x[i] > x0) {
j = i-1;
break;
}
}
double hj = x[j+1] - x[j];
double aj = (x[j+1] - x0) / hj;
double bj = (x0 - x[j]) / hj;
double cj = (aj*aj*aj - aj)*hj*hj / 6;
double dj = (bj*bj*bj - bj)*hj*hj / 6;
return aj*y[j] + bj*y[j+1] + cj*z[j] + dj*z[j+1];
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("Select a method:");```
Posted
Updated 15-Feb-23 6:48am
v2
Comments
CHill60 15-Feb-23 11:35am
And what is wrong with your code?
Aya Rh 15-Feb-23 11:48am
it's wrong
Richard Deeming 15-Feb-23 11:56am
Then fix it.

If you can't be bothered to explain the problem, then why should anyone bother trying to help you?

## Solution 1

Quote:
it's wrong

Tells us nothing.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

And to be honest, I'm not wading through that undocumented, single character variable code to try and work out how it is supposed to work even if you did tell us what was wrong with it: it's student grade code.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 50 BupeChombaDerrick 40 Graeme_Grant 10 Member 16273140 10 Pete O'Hanlon 10
 Pete O'Hanlon 1,750 OriginalGriff 1,357 Richard Deeming 550 Dave Kreskowiak 473 Richard MacCutchan 355

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900