Confuse with function handles, matrix and single point

1 view (last 30 days)
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

Accepted Answer

Amit
Amit on 12 Jan 2014
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 Comment
Bac VU
Bac VU on 12 Jan 2014
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!