Hi! Can you guys help mi with this code? The error is "Undefined function 'plus' for input arguments of type 'function_handle'."

조회 수: 6 (최근 30일)
clc; clear;
rect = @(n) rectangularPulse(-0.5,0.5,n);
sum = 0;
for i=0:7
a = @(n) (rect((n-19*i)/7));
sum = sum + a;
fplot(sum,[0,500]);
end

채택된 답변

Steven Lord
Steven Lord 2016년 11월 6일
You cannot add a function handle and a number or two function handles.
You can add the value returned by evaluating a function handle and a number.
fh1 = @(x) x.^2;
y = 1 + fh1(2) % works
y = 1 + fh1 % does not work
If you need a function handle as the output:
fh2 = @(x) x+1;
fh3 = @(x) sqrt(x);
fh4 = @(x) fh2(x) .* fh3(x)
This last is equivalent to:
fh4a = @(x) (x+1) .* sqrt(x)
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 11월 7일
편집: Walter Roberson 2016년 11월 7일
fplot() is to plot functions given their handle or symbolic expression. You are passing in a numeric scalar, c, instead.
for i=0:7
a = @(n) (rect((n-19*i)/7));
c(i+1) = a(i);
end
plot(0:7, cumsum(c));

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 11월 6일
Felipe - the error message is due to the line
sum = sum + a;
where you are trying to add a running total (named sum which is a poor choice of a local variable name since it conflicts with a MATLAB built-in function) with an anonymous function a
a = @(n) (rect((n-19*i)/7));
So the error message makes sense. How should you be evaluating a? What should n be?
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2016년 11월 6일
What should n be? Why have you chosen i to run from zero to seven?
Felipe neut
Felipe neut 2016년 11월 6일
편집: Felipe neut 2016년 11월 6일
Oo sorry.. the problem is for l from 0 to 7, M=19, N=7. I have no more datas in the problem, and should achieve to this graph. Thanks again really :)

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by