Root-Finding method in MATLAB

In summary, the script tries to find the root of y using the Newton-Raphson Method, but it keeps going into an infinite loop.
  • #1
ShakeSpee
5
0

Homework Statement


Compute and plot the compressibility factor (y) verses pressure (x) for the (1) Van der Waal’s (2) Redlich-Kwong and (3) Peng-Robinson equations of state.

Compressibility Factor, Z = (P*v)/(R*T); where v is the specific volume (V/v).

Data for n-Butane:
T = 500 K; Tc = 425.2 K; Pc = 37.5 atm; R = 0.08206 L.atm/(mol.K);
for P = 1:31 (atm)
Equations of State:

Hint:
1. Make P, Pc, T, Tc, and R global variables
2. Write three functions. One for each of the equations.
3. Write a script file that calls the functions using a root finding method to determine a root
4. Use the root to calculate the compressibility factor
5. Plot the compressibility factor verses pressure.
Note: Show all three graphs in the same plot window. Properly label the axis etc.

Homework Equations



Van der Waal[/B]:
a = 0.42188*(R^2*Tc^2/Pc)
b = 0.125(RTc/Pc)

Pv^3 - (Pb + RT)*v^2 + (a)v + ab = 0.

Redlich-Kwong:
Tr = T/Tc;
alpha = 1/(Tr^0.5);
a = 0.42748*(((R^2)*(Tc^2))/Pc)*alpha;
b = 0.08664*((R*Tc)/Pc);

(P)*(x^3) - (R*T)*(x^2) + (a - P*(b^2) - R*T*b)*x - a*b;

Peng- Robinson:
w = 0.193;
m = 0.37464 + 1.54226*w - 0.26992*w^2;
Tr = T/Tc;
alpha = (1 + m*(1 - (Tr^0.5)))^2;
a = 0.45724*(((R^2)*(Tc^2))/Pc)*alpha;
b = 0.07780*((R*Tc)/Pc);

(P)*(x^3) + (b*P - R*T)*(x^2) + (a -3*P*(b^2) - 2*R*T*b)*(x) + (P*(b^3) + R*T*(b^2) - a*b);

The Attempt at a Solution


[/B]
I finished the first two steps - I created function scripts for all of the equations, but I'm stuck on the third part, which is finding the root of one of the functions. I can use any method to find the root, and for now, I chose the Newton-Raphson Method, so I also created scripts for the derivatives of each function. Whenever I run this script, it goes into an infinite loop, and I can't figure out why. Here is what I have so far:%This script will take a user input (guess) and use the Newton - Raphson
%Method to determine the root of the function. y = vdw(x) and ypr =
%vdwpr(x), the derivative of vdw(x).
T = 500;
Tc = 425.2;
Pc = 37.5;
R = 0.08206;

%Set converge tolerance.
tol = 0.00001;

%Input initial guess.
x = input('Enter initial guess: ');

y = vdw(x);

while abs(y) > tol;
for P = 1:31
y = vdw(x);
ypr = vdwpr(x);
end
x = x - (y/ypr);
end
 
Physics news on Phys.org
  • #2
It doesn't look like you're setting abs(y) to anything in your while loop.
 
  • #3
TJGilb said:
It doesn't look like you're setting abs(y) to anything in your while loop.
You don't "set abs(y)" to a value -- you set y, and take its absolute value, which the OP is doing in the while loop.
Matlab:
while abs(y) > tol;
    for P = 1:31
       y = vdw(x);  ;; <---  y gets a new value here
      ypr = vdwpr(x);
    end
    x = x - (y/ypr);
end
 

1. What is the root-finding method in MATLAB?

The root-finding method in MATLAB is a mathematical algorithm used to find the roots of a given function. The roots of a function are the values of the independent variable that make the function equal to zero.

2. How does the root-finding method work in MATLAB?

The root-finding method in MATLAB works by using an iterative process to approximate the roots of a function. It starts with an initial guess and then uses a series of calculations to refine the guess until it reaches a desired level of accuracy.

3. What are the advantages of using the root-finding method in MATLAB?

The root-finding method in MATLAB is advantageous because it can be applied to a wide range of functions, including non-linear and multi-dimensional functions. It is also a relatively fast and efficient method for finding roots compared to other methods.

4. What are the limitations of the root-finding method in MATLAB?

The root-finding method in MATLAB may not always converge to the correct root, especially for functions with multiple roots or when the initial guess is too far from the actual root. It also requires a good initial guess and may not work well for functions with discontinuities or sharp changes.

5. How can I use the root-finding method in MATLAB for my own functions?

To use the root-finding method in MATLAB for your own functions, you can define your function as a MATLAB function file and then use the built-in root-finding functions such as fzero or fsolve. You can also create your own custom algorithm using loops and conditional statements to find the roots of your function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top