# Help with simple nested function

2 Ansichten (letzte 30 Tage)
Davide Cannavacciuolo am 3 Nov. 2023
Bearbeitet: Sam Chak am 3 Nov. 2023
The function i am trying to get is:
%first mode
function g=hall(t)
lolla=seno(t);
function h=seno(t);
h=sin(t);
end
if lolla>0
g=1;
else g=0;
end
end
that means if the sine is positive the function outputs 1, otherwise negative. I get serveral types of errors. Edit as you prefer and make it even simple if you can. Later on I will have to edit the sine function with other functions of time. Thank you very much in advance.
##### 6 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Dyuman Joshi am 3 Nov. 2023
"yes you are right, but when I do fplot(hall,[0 3]) for exaple i get error, know why?"
First of all, the syntax of calling the function is incorrect. See my comment above for the correct syntax.
Secondly, fplot expects function handles as input, whereas the function you have provides numbers as outputs.
What is the objective here? Do you want to plot the outcome of the function for different values?
If so, then which type of plot? and what to plot against?
Davide Cannavacciuolo am 3 Nov. 2023
Bearbeitet: Davide Cannavacciuolo am 3 Nov. 2023
I need a simple matlab function that can do something like this:

Melden Sie sich an, um zu kommentieren.

### Antworten (3)

Jon am 3 Nov. 2023
Your code seems to run without errors as demonstrated below, whether it does what you want is another question. What is it exactly you are trying to do?
t = linspace(0,2*pi);
g = hall(t)
g = 0
%first mode
function g=hall(t)
lolla=seno(t);
function h=seno(t);
h=sin(t);
end
if lolla>0
g=1;
else g=0;
end
end
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Jon am 3 Nov. 2023
Bearbeitet: Jon am 3 Nov. 2023
To make a plot that is similar to the Simulink block diagram that you show you could use this
t = linspace(0,10*pi,1000);
x = sin(t);
% x>0 is logical array true (1) when x is positive false (0), make it into
% numerical values of ones and zeros so that you can plot it, or otherwise
% maninpulate it
xpm = double(x>0);
plot(t,xpm)
Jon am 3 Nov. 2023
Note, this will also work with x assigned to other functions of time, I am just using sin(t) as it matches your example.

Melden Sie sich an, um zu kommentieren.

Steven Lord am 3 Nov. 2023
Verschoben: Dyuman Joshi am 3 Nov. 2023
The command fplot(hall, [0 3]) attempts to call hall with no input arguments and use what it returns as the first input to the fplot function. Your hall function requires an input to operate correctly.
If you were to specify a function handle to the hall function (to allow fplot to call it with the inputs it decides to use) you'd still have an issue when your user (or fplot) calls your function with a non-scalar input. That would look like fplot(@hall, [0 3]). Instead of using if in your hall code, you'd probably want to use logical indexing.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Dyuman Joshi am 3 Nov. 2023
@Davide Cannavacciuolo, follows Steven's advice to
- understand syntax of fplot() function for plotting
- and use logical indexing to modify your code so that it works for non-scalar inputs

Melden Sie sich an, um zu kommentieren.

Sam Chak am 3 Nov. 2023
Bearbeitet: Sam Chak am 3 Nov. 2023
If I'm not mistaken, you probably want to plot something like this, right? Once you confirm that, we can guide you how to plot the graph using your Nested function. By the way, is it a requirement to use fplot()?
t = linspace(0, 2*pi, 3601);
g = zeros(1, numel(t));
for j = 1:numel(t)
g(j) = hall(t(j));
end
plot(t/pi, sin(t)), hold on
plot(t/pi, g), grid on, ylim([-1.5 1.5])
xlabel('t/{\pi}')
legend('sin(t)', 'g')
%% first mode
function g = hall(t)
lolla = seno(t);
function h = seno(t);
h = sin(t);
end
if lolla > 0
g = 1;
else
g = 0;
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by