Errors in using multiple functions in matlab

In summary: I have fixed the problem and the program now works. In summary, the problem was that I was passing in the wrong functions to Newton1.
  • #1
adeeyo
20
0
Hi,
Please I need your assistance in solving this MATLAB problem. Thanks for your anticipated help

I have three functions

First one


function f_PR_W=Newtonphasemolefraction1(W_PR, z,C,K_PR )
f_PR=0;
for c=1:C

f_PR_W=f_PR+z(c).*(1-K_PR(c))./(1+W_PR.*(K_PR(c)-1));
end




Another function


function fdot_PR_W=Newtonphasemolefraction2(W_PR, z,C,K_PR )
fdot_PR=0;

for c=1:C
fdot_PR_W=fdot_PR+z(c).*(K_PR(c)-1).^2/(W_PR.*(K_PR(c)-1)+1).^2;

end

The third function

function [W_PR,err,k,f_PR_W]=Newton1(Newtonphasemolefraction1,Newtonphasemolefraction2,W_PR,Tol_W_PR,Tol_f_PR,maxIter)


for k=1:maxIter
p1=W_PR-Newtonphasemolefraction1(W_PR)/Newtonphasemolefraction2(W_PR);
err=abs(p1-W_PR);
relerr=2*err/(abs(p1)+Tol_W_PR);
W_PR=p1;
f_PR_W=Newtonphasemolefraction1(W_PR);
if (err<Tol_W_PR)|(relerr<Tol_W_PR)|(abs(f_PR_W)<Tol_f_PR),break,end
end


I call the three functions using

[W_PR,err,k,f_PR_W]=Newton1(Newtonphasemolefraction1,Newtonphasemolefraction2,W_PR,Tol_W_PR,Tol_f_PR,maxIter)

I received the following error

"Error using Newtonphasemolefraction1 (line 3)
Not enough input arguments."

And all input arguments were defined: K_PR=[0.01546,0.5,1.432],W_PR=0.25,Tol_W_PR=10^-6, Tol_f_PR=10^-10, maxIter=100, z=[0.1 0.3 0.6],C=3

Please assist.


adeeyo
 
Physics news on Phys.org
  • #2
Well...I don't know matlab, so allow me to just indicate generic stuff that does not seem to be correct in your program.

First, you declare Newtonphasemolefraction1(W_PR, z,C,K_PR ); that is, this function takes 4 arguments...yet, when you are using it inside of Newton1, you call it with only 1 argument...it is line 3 within the function Newton1, so maybe that is the error you are getting.

By the way, do you really need to pass the first two functions as arguments to the third one so that you can use them inside of it?...I doubt that very much, that is not the case for many other programming languages.
 
  • #3
Gsal

Thanks for your reply
 

Related to Errors in using multiple functions in matlab

1. What are the most common types of errors when using multiple functions in MATLAB?

The most common types of errors when using multiple functions in MATLAB include syntax errors, logical errors, and runtime errors. Syntax errors occur when there is a mistake in the code, such as missing parentheses or incorrect use of operators. Logical errors occur when the code runs but produces incorrect results due to a mistake in the logic of the program. Runtime errors occur when the code encounters an unexpected problem during execution, such as trying to access a variable that does not exist.

2. How can I prevent errors when using multiple functions in MATLAB?

To prevent errors when using multiple functions in MATLAB, it is important to carefully check the syntax of your code before running it. You can also use the debugging tools in MATLAB, such as the debugger and error messages, to identify and fix any errors. Additionally, using good coding practices, such as commenting your code and breaking it into smaller, modular functions, can help prevent errors.

3. How do I handle errors that occur when using multiple functions in MATLAB?

To handle errors that occur when using multiple functions in MATLAB, you can use the try and catch statements. These allow you to specify a block of code to be executed if an error occurs, and handle the error in a specific way. You can also use the error function to create custom error messages that provide information about the error and how to fix it.

4. Can I use multiple functions from different files in MATLAB?

Yes, you can use multiple functions from different files in MATLAB. This is known as function nesting, where one function calls another function to perform a specific task. To use functions from different files, you can either add the files to your current working directory or use the addpath function to add the directory containing the files to MATLAB's search path.

5. How do I know which function is causing an error when using multiple functions in MATLAB?

If an error occurs when using multiple functions in MATLAB, the error message will usually indicate which function the error occurred in. You can also use the debugging tools in MATLAB, such as the stack trace, to see the sequence of function calls that led to the error. Additionally, you can use the dbstop function to set breakpoints in your code and step through it to identify the source of the error.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
963
Back
Top