필터 지우기
필터 지우기

Confuse with function handles, matrix and single point

조회 수: 1 (최근 30일)
Bac VU
Bac VU 2014년 1월 11일
댓글: Bac VU 2014년 1월 12일
Hello, I got homework with this a little bit complex function.
I calculate values of the function with input is a vector and single points. Same function but the results are pretty difference!!! (See on the graph.
Anyone can explain it?. Thanks a lot!
Here 's my image and code .
x =-2:.01:2;
f =@(x) x.*sqrt(x.^4+1).*log(4-x.^2)+...
6*sin(4+x.^2)/(1+exp(-x))-...
7*x;
%----------
grid on
hold on
plot(x,f(x),'*r-')
%------------------
for i=1:length(x)
plot(x(i),f(x(i)),'o')
end
%---------------------------------------------------
% ff = x.*sqrt(x.^4+1).*log(4-x.^2)+...
% 6*sin(4+x.^2)/(1+exp(-x))-...
% 7*x;
% plot(x,ff,'g')
% % -----------------------------------------
% for i=1:length(x)
% xx=x(i);
% fff = xx.*sqrt(xx.^4+1).*log(4-xx.^2)+...
% 6*sin(4+xx.^2)/(1+exp(-xx))-...
% 7*xx;
% plot(xx,fff,'y*')
% end

채택된 답변

Amit
Amit 2014년 1월 12일
In defining f, you didn't do the function right. To take a value of x as a vector, you need to use ./ instead of just / . In think the proper code will be
x =-2:.01:2;
f =@(x) x.*sqrt(x.^4+1).*log(4-x.^2)+...
6*sin(4+x.^2) ./ (1+exp(-x))-...
7*x;
This will be same for both scaler and vector inputs.
  댓글 수: 1
Bac VU
Bac VU 2014년 1월 12일
Thanks Amit for the helpful question. It works perfectly,
Matlab usually return error "dimension mismatch" when I try to multiply vectors, so I didn't note that Matlab do divides same length vector in this case.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by