Confuse with function handles, matrix and single point
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!