MATLAB Answers

Please explain what this error is...

1 view (last 30 days)
Hossein Baglari
Hossein Baglari on 21 Nov 2020
Edited: dpb on 21 Nov 2020
[Mistakenly put Q? in Title moved to Body by dpb]
Hi, I'm very new to MATLAB and I am having some trouble. Lots of people have had the same problem but nobody seems to be able to explain or solve it in plain English. Could somebody please explain what this error is and how to fix it? I have a simpl...
function [eq1,eq2,eq]= Myfun(a,b)
m0=9.1094e-31;
me=.7*m0;
q=1.60218e-19;
k=1.38064852e-23;
Tc=300;%kelvin
KT=k*Tc;
h_plank=6.626069572929e-34;
hbar=h_plank/(2*pi);
c=3e8;
omega=6.8e-6;
V=1*q;
Ts=5678;
KT2=k*Ts;
ph=h_plank*pi;
hc=h_plank^3*c^2;
filename='am15.txt';
As=importdata(filename);
lambda=As(1,:)*1e-9;
x=h_plank*c./lambda; % x is E
% M(1) is T_H and M(2) is delta_mu
Delta_E=0.01;
Ext_E=1.7;
for iii=1:length(x)
if (Ext_E/2-Delta_E/2) < x(iii) < (Ext_E/2+Delta_E/2)
% if x(iii) >= (Ext_E/2-Delta_E/2) && x(iii) <= (Ext_E/2+Delta_E/2)
tau=1;
elseif x(iii) >= (Ext_E/2+Delta_E/2) || x(iii) <= (Ext_E/2-Delta_E/2)
tau=0;
end
end
eq1=trapz(x,tau*(me*k*a/ph)*log10(1+exp(-(x-b/2)./k*a)));
eq2=trapz(x,tau*(me*KT/ph)*log10(1+exp(-(x-V/2)./KT)));
eq3=trapz(x,(2*omega/hc)*(x.^2./(exp(x/KT2)-1)));
eq4=trapz(x,(2*pi/hc)*(x.^2./(exp((x-b/2)/k*a)-1)));
eq1=(1/(pi*hbar))*(eq1-eq2)-eq3+eq4;
eq11=(trapz(x,tau*(me*k*a/ph)*(x+a*k-b/2).*log10(1+exp(-(x-b/2)./k*a))));
eq22=trapz(x,tau*(me*KT/ph)*((x+KT-V/2)).*log10(1+exp(-(x-V/2)./KT)));
eq33=trapz(x,(2*omega/hc)*(x.^3./(exp(x/KT2)-1)));
eq44=trapz(x,(2*pi/hc)*(x.^3./(exp((x-b/2)./k*a)-1)));
eq2=(1/(pi*hbar))*(eq11-eq22)-eq33+eq44;
eq=[eq1 eq2];
end
when I run this program that matlab shows this error
>> Myfun
Not enough input arguments.
Error in Myfun (line 36)
eq1=trapz(x,tau*(me*k*a/ph)*log10(1+exp(-(x-b/2)./k*a)));

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 21 Nov 2020
Edited: dpb on 21 Nov 2020
That's pretty straightforward problem and the error is quite simple to fix --
Your function definition line is
function [eq1,eq2,eq]= Myfun(a,b)
which says your function Myfun requires two input arguments, namely, variables you've named a, b (Not very informative of what they are, perhaps, but that's only an aid to the programmer to use meaningful variable names, MATLAB itself doesn't care).
You then tried to execute the function with the line
>> Myfun
without any input arguments passed for the two required arguments.
Line 36
eq1=trapz(x,tau*(me*k*a/ph)*log10(1+exp(-(x-b/2)./k*a)));
is the first line in the function that needs/references a and b and so throws the error that they're not defined because you didn't pass the required two arguments.
The fix is to call the function with two arguments for those two needed variables...whatever values they're supposed to have.
a=1; % make up some values for needed parameters
b=2;
Myfun(a,b)

  2 Comments

dpb
dpb on 21 Nov 2020
It would have been more clear what the problem was if you had checked for the needed arguments at the beginning of the function and either thrown the error then or, if wanted to make an easy-to-use function wherein the arguments aren't needed because they are generally a fixed set of values, provided default values.
function [eq1,eq2,eq]= Myfun(a,b)
if nargin<2,error('Must supply two arguments a,b'),end
m0=9.1094e-31;
me=.7*m0;
...
is the hammer solution...there are examples in the documentation of more subtle ways to handle the problem.
As this shows, it's always good to code defensively...
Hossein Baglari
Hossein Baglari on 21 Nov 2020
thank you very much for helping

Sign in to comment.

More Answers (1)

Stephan
Stephan on 21 Nov 2020
Edited: Stephan on 21 Nov 2020
Your function has to be called with 2 inputs a and b for example when a=2 and b=5:
[res1, res2, res3] = MyFun(2, 5)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!