Adding functions in a for loop

I am trying to add a function in a for loop but I keep getting an error (Undefined function 'plus' for input arguments of type 'function_handle'). I would like to keep summing y(x) and plot the final result. Below is my script. Thanks ahead of time!
syms x
y = @(x) 1
for c = 1:5;
y = y(x) + @(x) 3*c.*((0 <= x) & (x <= 5))
x = linspace(0.2, 4, 500);
end
plot(x,y(x)

1 Kommentar

One thing you can do is to define the second function (y2 = @(x) 3*c.*((0 <= x) & (x <= 5)) before that summation. I do not think Matlab allows defining functions in an algebraic equation.
Secondly, when you define
y = y(x);
you are basically overwriting the y(x) function and the next time you call y(x), it will give you an error.
There might be an easier way of doing this. If you actually type your question, someone might write the code for you.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

OCDER
OCDER am 19 Jul. 2018

0 Stimmen

Error is caused by adding a function handle to a value returned by a function handle. They're different things.
Y = y(x) + @(x) x^2 for instance, is equivalent to saying
value return by y(x) + a function name. Matlab goes : huh? What is 3 + name = ???
You don't need syms either it seems. You can do the following:
x = linspace(0.2, 4, 500);
y = @(x) 1; %This just returns 1 regardless of x. Is this what you want?
for c = 1:5
YH = @(x) y(x) + 3*c.*((0 <= x) & (x <= 5));
plot(x,YH(x))
end
I'm confused as to what you're expecting to plot, other than a line. Recheck YH, the function handle.

5 Kommentare

Jeff
Jeff am 19 Jul. 2018
I was just using an easy example; which is why I chose to plot a line. I want a function y(x) to be calculated and added to its previous value on every iteration.
In other words, the output of this loop would be y(1) + y(2) + y(3) + y(4) + y(5). At least that is my intent. Hope that makes sense. Thanks for the input!
OCDER
OCDER am 19 Jul. 2018
I see. In that case, something like this might work.
x = linspace(0.2, 4, 500);
y = @(x) 1; %This just returns 1 regardless of x. Is this what you want?
for c = 1:5
YH = @(x) y(x) + 3*c.*((0 <= x) & (x <= 5));
if c == 1
y_all = YH(x);
else
y_all = y_all + YH(x);
end
end
plot(x,y_all)
Stephen23
Stephen23 am 19 Jul. 2018
Bearbeitet: Stephen23 am 19 Jul. 2018
"the output of this loop would be y(1) + y(2) + y(3) + y(4) + y(5)"
Adding function handles in a loop seems a pointlessly complex way to achieve that, especially as the function you are adding is always the same. Why not just:
  • define one function before the loop, and call that inside the loop (or use arrayfun).
  • write efficient vectorized code?
OCDER
OCDER am 19 Jul. 2018
Yeah, I agree with Stephen - making a ton of function handles is not the right way to go. Making a function that takes input x and c would be better. Example:
x = linspace(0.2, 4, 500);
y = @(x) 1; %This just returns 1 regardless of x. Is this what you want?
YH = @(x, c) y(x) + 3*c.*((0 <= x) & (x <= 5));
for c = 1:5
if c == 1
y_all = YH(x, c);
else
y_all = y_all + YH(x, c);
end
end
plot(x,y_all)
Jeff
Jeff am 25 Jul. 2018
Thanks for the input! I was able to get my code to work.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Jul. 2018

Kommentiert:

am 25 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by