Errors in using multiple functions in matlab

Click For Summary
SUMMARY

The forum discussion centers on a MATLAB error encountered when using multiple functions. The user defined three functions: Newtonphasemolefraction1, Newtonphasemolefraction2, and Newton1. The error "Not enough input arguments" arises because Newtonphasemolefraction1 is called with only one argument within Newton1, despite requiring four. The discussion highlights the importance of correctly passing function arguments in MATLAB.

PREREQUISITES
  • Understanding of MATLAB function syntax
  • Familiarity with function argument passing in MATLAB
  • Basic knowledge of numerical methods, specifically Newton's method
  • Experience with MATLAB data structures, such as arrays
NEXT STEPS
  • Review MATLAB function argument passing conventions
  • Learn about debugging techniques in MATLAB to identify function call issues
  • Study numerical methods, particularly Newton's method for root finding
  • Explore MATLAB's error handling and messaging system for better troubleshooting
USEFUL FOR

MATLAB programmers, numerical analysts, and anyone troubleshooting function-related errors in MATLAB code.

adeeyo
Messages
20
Reaction score
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
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.
 
Gsal

Thanks for your reply
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K