Effacer les filtres
Effacer les filtres

Confuse with function handles, matrix and single point

1 vue (au cours des 30 derniers jours)
Bac VU
Bac VU le 11 Jan 2014
Commenté : Bac VU le 12 Jan 2014
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

Réponse acceptée

Amit
Amit le 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 commentaire
Bac VU
Bac VU le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by