- what is the valuke of y for 30<ok<60 ?
- you will need to call the nested function with an output argument, otherwise there is absolutely no point in defining y inside that function.
- you will need to use a loop, or use arrayfun, or vectorize your code (best):
Writing a recursive function with nested function /
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 7 Okt. 2020
Bearbeitet: Stephen23
am 7 Okt. 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.
Antworten (1)
Aksel Bektas
am 8 Okt. 2020
1 Kommentar
Walter Roberson
am 8 Okt. 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).
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!