Using multiple functions in matlab Errors

In summary, the conversation discusses a problem with a MATLAB code related to three functions, Newtonphasemolefraction1, Newtonphasemolefraction2, and Newton1. The error message "Not enough input arguments" is received, and the conversation mentions that all input arguments were defined. The expert summarizer notes that in many computer languages, arguments must be explicitly stated when calling a function, and suggests a potential solution to the problem.
  • #1
adeeyo
20
0
Dear All,

Please I need your assistance in solving this MATLAB problem. I will be glad to hear from you as early as possible. 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,Newtonphasemolef raction2,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,Newtonphasemolef raction2,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
adeeyo said:
function f_PR_W=Newtonphasemolefraction1(W_PR, z,C,K_PR )
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)

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

I don't know Matlab, but in most computer languages your would have to write something like:

[W_PR,err,k,f_PR_W]=Newton1(Newtonphasemolefraction1(a,b,c,d),
Newtonphasemolefraction2(e,f,g,h),.. etc.

because you can't assume the compiler knows what arguments you want to pass to the functions Newtonphasemolefraction1 and Newtonphasemolefraction2 if you don't tell it. It looks like the compiler is objecting that you didn't supply any arguments to those functions.
 

What is a function in matlab?

A function in matlab is a block of code that performs a specific task and can be called from other parts of the code. It helps in organizing and simplifying complex tasks.

How do you define a function in matlab?

A function in matlab is defined using the keyword "function" followed by the name of the function, input arguments (if any), and the keyword "end" to mark the end of the function. The code for the function is written in between these keywords.

What is an error in matlab?

An error in matlab is an issue or mistake in the code that prevents it from running correctly. It can be caused by various factors such as incorrect syntax, missing variables, or logical errors.

How do you debug multiple functions in matlab?

There are several methods to debug multiple functions in matlab. One way is to use the "dbstop if error" command, which stops the code at the line where the error occurs, allowing you to examine the values of variables and identify the cause of the error. Another way is to use the "dbstep" command, which allows you to step through the code line by line and track the flow of execution.

How can you handle errors in multiple functions in matlab?

To handle errors in multiple functions in matlab, you can use the "try-catch" statement. This allows you to try executing a piece of code and catch any errors that may occur. You can then handle the error by displaying a message or taking appropriate actions to resolve it.

Similar threads

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