How do I add anonymous functions together?

4 visualizzazioni (ultimi 30 giorni)
Dylan
Dylan il 26 Ago 2022
Modificato: Matt J il 26 Ago 2022
What I would like to achieve is a for loop with n iterations that lengthens a function after each iteration, but I'm not sure what the syntax is to add two anonyous functions
Sample code:
for n = 1 : iterations
function = function + new_function
end
where 'function' and 'new_function' are anonymous functions
Desired output:
if function = @(x) x.^2
and new_function = @(x) x.^3
function + new_function = x.^2 + x.^3

Risposta accettata

Matt J
Matt J il 26 Ago 2022
Modificato: Matt J il 26 Ago 2022
I was hoping for something along these lines:
for n = 0:5
new_func = @(x) (((-1)^n).*((x).^(2*n)))./(factorial(2*n))
final_func = @(x) final_func(x) + new_func(x)
end
After the for loop, I'd like to output to look smiliar to this:
final_func = @(x) (((-1)^0).*((x).^(2*0)))./(factorial(2*0)) + (((-1)^1).*((x).^(2*1)))./(factorial(2*1)) + (((-1)^2).*((x).^(2*2)))./(factorial(2*2));
No, not without string manipulation.
The function generated by your loop will return the correct values (and is equivalent to @Stephen23's answer), but it is grossly inefficient. You could get to a much better implementation of final_func in one line by using vectorization.
n=0:5;
final_func=@(x) sum( (((-1).^n).*((x).^(2*n)))./(factorial(2*n)) );
  2 Commenti
Dylan
Dylan il 26 Ago 2022
Spostato: Matt J il 26 Ago 2022
Thanks to @Matt J for help with the solution
Here's my final code
How do I avoid the Warning?
n=0:5;
final_func=@(x) sum( (((-1).^n).*((x).^(2*n)))./(factorial(2*n)) )
final_func = function_handle with value:
@(x)sum((((-1).^n).*((x).^(2*n)))./(factorial(2*n)))
fplot(final_func)
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
axis([-pi pi -1 1])
Matt J
Matt J il 26 Ago 2022
Spostato: Matt J il 26 Ago 2022
How do I avoid the Warning?
n=(0:5)';
final_func=@(x) sum( (((-1).^n).*((x).^(2*n)))./(factorial(2*n)) ,1);
fplot(final_func)
axis([-pi pi -1 1])

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 26 Ago 2022
Modificato: Matt J il 26 Ago 2022
Trying to sum/concatenate anonymous functions is a bad thing to do, in general. Here is one way to accomplish it, however,
fstr=@(z) string( extractAfter(func2str(z),'@(x)') );
func="";
for n = 1 : iterations
func= func + fstr(new_function);
end
func=str2func( "@(x)"+func )
  2 Commenti
Dylan
Dylan il 26 Ago 2022
Is this possible without converting the anonymous function to a string?
I was hoping for something along these lines:
for n = 0:5
new_func = @(x) (((-1)^n).*((x).^(2*n)))./(factorial(2*n))
final_func = @(x) final_func(x) + new_func(x)
end
After the for loop, I'd like to output to look smiliar to this:
final_func = @(x) (((-1)^0).*((x).^(2*0)))./(factorial(2*0)) + (((-1)^1).*((x).^(2*1)))./(factorial(2*1)) + (((-1)^2).*((x).^(2*2)))./(factorial(2*2));
where for each term, the n value is substituted
In the end, I'd like to plot final_func using fplot
Stephen23
Stephen23 il 26 Ago 2022
Modificato: Stephen23 il 26 Ago 2022
" I'd like to output to look smiliar to this: final_func = @(x) (((-1)^0).*((x).^(2*0)))./(factorial(2*0)) + (((-1)^1).*((x).^(2*1)))./(factorial(2*1)) + (((-1)^2).*((x).^(2*2)))./(factorial(2*2));"
Adding functions is a red-herring. You should be using arrays, not adding functions in a loop.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by