Matlab If Else Statement

2 visualizaciones (últimos 30 días)
Joseph DeMaria
Joseph DeMaria el 2 de Nov. de 2020
Respondida: VBBV el 4 de Oct. de 2024
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!

Respuestas (2)

ag
ag el 3 de Oct. de 2024
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 el 4 de Oct. de 2024
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

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by