Writing a recursive function with nested function /

7 visualizaciones (últimos 30 días)
Aksel Bektas
Aksel Bektas el 7 de Oct. de 2020
Comentada: Walter Roberson el 8 de Oct. de 2020
Hey,
For a course project, I figured it was best to use matlab to plot a few cost functions. I've programmed before in c++ and scala, but Matlab syntax is causing me a headache. I want to plot the following function( just a line connecting the points) from 0 to 900. It's working as I want but for some reason im not able to plot it.
Basically my question is, how to plot the given function?
Help would be greatly appreciated.
function [ y] =recursiona(t)
total= 0;
function y=plot(ok)
if(ok>= 520)
y=50;
return
end
if(ok<=0)
y=total;
return
end
if(ok >= 60)
total =total+ 9;
plot( ok- 60)
elseif( ok <= 24)
total = total + 0.25 * ok;
y= total;
return
elseif(ok >= 24 && ok <= 30 )
total=total + 6;
y=total;
return
elseif( ok > 30 && ok <= 42)
total =6 + (ok - 30) * 0.25;
else
total = total + 9;
total;
return
end
end
plot(t)
end
And my plotting attempt
x= 0:900;
y=recursiona(x);
plot(y,x);
I receive the following error in cmd.
Operands to the || and && operators must be convertible to logical scalar values.
Error in recursiona/plot (line 23)
elseif(ok >= 24 && ok <= 30 )
Error in recursiona (line 38)
plot(t)
Error in test (line 2)
y=recursiona(x);
  4 comentarios
Stephen23
Stephen23 el 7 de Oct. de 2020
Editada: Stephen23 el 7 de Oct. de 2020
"w.r.t to you second point. I'm not sure i understood you. I am calling the function 'plot' ..."
Yes you are calling it, but without any output argument. This is how you define the function (with one output y):
function y=plot(ok)
and this is how you call the function (without any output argument):
plot(t)
You call plot with NO output argument, so any y value calculated inside the function is simply discarded.
Tip: to avoid problems later plotting data, you should avoid using the name plot.
"w.r.t the third point, why my current implementation isn't adequate? "
Within the function there is no indexing at all nor any attempt at code vectorization, so your code seems to be written on the assumption that its inputs are scalar values. If you want to apply a function that only works on scalar values to a vector of values then you will need to use a loop... or rewrite the code to work on vectors properly (best option).
As I wrote in my last comment, you need to learn how to handle vectors/matrices/arrays. That is the entire point of MATLAB.

Iniciar sesión para comentar.

Respuestas (1)

Aksel Bektas
Aksel Bektas el 8 de Oct. de 2020
Thanks again,
I got it working. There were quite a few issues, namely, that return behaves quite differently in Matlab. Apparently, if a function returns some value, it may not be written to the output of the function. Thus, I got rid of all returns. Working code below.
function [ y] =recursiona(t)
total= 0;
y= plot(t);
function as=plot(ok)
if(ok>= 320)
as=50;
elseif(ok<=0)
as=total;
elseif(ok >= 60)
total =total+ 9;
as=plot( ok- 60);
elseif( ok <= 24)
total = total + 0.25 * ok;
as= total;
elseif(ok >= 24 && ok <= 30 )
total=total + 6;
as=total;
elseif( ok > 30 && ok <= 42)
total =total + 6 + (ok - 30) * 0.25;
as=total;
else
total = total + 9;
as=total;
end
end
end
  1 comentario
Walter Roberson
Walter Roberson el 8 de Oct. de 2020
Apparently, if a function returns some value, it may not be written to the output of the function.
In MATLAB, values returned are always written to the output of the function.
The differences in that in MATLAB, return does not take an argument saying what value to return (as is the case in C). Also, functions do not return the last expression they executed (as in Maple).

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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