Matlab If Else Statement

3 次查看(过去 30 天)
Joseph DeMaria
Joseph DeMaria 2020-11-2
回答: VBBV 2024-10-4
The prompt of the question I have to do is: " The value of y is defined in terms of x by y=ln(0.5-x), write a function myfun(x) that takes some value x and does the following:
If x is greater or equal to 0.5 the function should caluculate y by using the negative value of that x and assign the result to z
If the value of x smaller than 0.5 the function should return the value z where z is calculated as the following
z=y^n
where n is (x+(100*x)) rounded to the nearest integer.
In my code, under a new scipt I created the function saved as "myfunx" as follows;
function myfun(x)
if x>=0.5
y=ln(0.5-(-x))
z=y^n
else
y=ln(0.5-x)
z=y^n
end
and in the command window when I test it with such value as x=1 and call the funciton myfunx, and it reads error and claims
"Not enough input arguments.
Error in myfunx (line2)
if x>=0.5
However I do not know what Im doing wrong or how I could fix this, any tips or ideas that could help me solve this would be appreciated thank you!

回答(2 个)

ag
ag 2024-10-3
Hi Joseph,
The error you are encountering is due to the function being executed without being invoked. To resolve this issue, please ensure that you call the function by passing the necessary parameters.
Additionally, it appears that the function "ln" is undefined in your code. If you intended to calculate the natural logarithm, please use the MATLAB function "log" as follows:
% y=ln(0.5-(-x))
y = log(0.5 - (-x));
The below code snippet demonstrates how to address these issues:
z = myfun(5)
z = 4.9543
function z = myfun(x)
% initializing "n" with dummy value
n = 3;
if x>=0.5
y = log(0.5 - (-x));
z = y^n;
else
y = log(0.5-x);
z = y^n;
end
end
Hope this helps!

VBBV
VBBV 2024-10-4
You have saved the script file with name myfunx but it also contains a function myfun(x) which takes input argument. You can either rename or change the script file name to myfun and then call it command window as
>> myfun(1)
OR
give input argument value to function inside the script file as below and then run the file using green button or using its name in command window without any argument. Since the script file is different from function file and does not take input arguments. See this link for more info
% run this file using the button
% add the below line inside the script file "myfunx"
myfun(1) % give a value here which calls the function myfun
function myfun(x)
n = 1; % define the value for n
if x>=0.5
y=log(0.5-(-x)) % use log for natural log function ... there is no ln in MATLAB
z=y^n
else
y=log(0.5-x)
z=y^n
end
end
% run this m-file at command window without any argument
>> myfunx
It seems your script file does not call function myfun(x) with input arguments and when you run the script file , it throws such error

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by