How do I add anonymous functions together?

조회 수: 3 (최근 30일)
Dylan
Dylan 2022년 8월 26일
편집: Matt J 2022년 8월 26일
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

채택된 답변

Matt J
Matt J 2022년 8월 26일
편집: Matt J 2022년 8월 26일
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
Dylan
Dylan 2022년 8월 26일
이동: Matt J 2022년 8월 26일
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 2022년 8월 26일
이동: Matt J 2022년 8월 26일
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])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2022년 8월 26일
편집: Matt J 2022년 8월 26일
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
Dylan
Dylan 2022년 8월 26일
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 2022년 8월 26일
편집: Stephen23 2022년 8월 26일
" 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by