how do you multiply f(-1)*f(0) in matlab?

Hey guys,
Annoying problem...but I'm new to matlab.
I have a function f=@(x)(x-2.5).*exp(-0.5)*(x-2).^2)+0.2. I want to calculate f(-1)*f(0). How do you do it?
I need to specify a bit. I'm confused on how to calculate f(x) for any number I want. Like, if I want the answer of f(8) of the equation how do I compute it in matlab?

 Accepted Answer

Try it this way:
workspace; % Make sure workspace panel is showing.
% Define function
f=@(x)(x-2.5).*exp(-0.5*(x-2).^2)+0.2
% Compute some results
r1 = f(8)
r2 = f(-1)
r3 = f(0)
r4 = r2 * r3

6 Comments

Thank you for the quick reply. I don't quite get your code since I can't get any results typing f(8). Matlab only replies "Undefined function 'f' for input arguments of type 'double'."
My function code is:
f=@(x)(x-2.5).*exp(-0.5*(x-2).^2)+0.2;
x=linspace(-2,7);
plot(x,f(x))
axis([-2 7 -1 1]), grid on
I want to make a=-1 and b=0 and I want to multiply the results of the function like this ---> f(a)*f(b)
I think I got it. If I wrote it under .m file of the function i defined then what I want works like perfect.
Works for me:
f=@(x)(x-2.5).*exp(-0.5*(x-2).^2)+0.2;
x=linspace(-2,7);
plot(x,f(x))
axis([-2 7 -1 1]);
grid on
f(8) % Type results of f(8) into command window
% Assign a and b
a = -1;
b = 0;
% Compute f(a)*f(b)
theAnswer = f(a) * f(b)
In the command window:
ans =
0.2000
theAnswer =
-0.0223
But your code shows that you have defined f in this line
f=@(x)(x-2.5).*exp(-0.5*(x-2).^2)+0.2;
What happens when you execute f(8) immediately after that line? Does f show up in the workspace panel which Image Analyst had you open?
Britney, I think our answers crossed and you got it working now. So please mark the answer as Accepted if we're done.
if I type f(8) in command window it works. I get the same answers as Image Analyst. I think i tried typing f(8) in a different script and that is why it didn't work. But it all works now.
Thank you guys for your help!!
xoxo

Sign in to comment.

More Answers (0)

Asked:

on 7 Oct 2014

Commented:

on 7 Oct 2014

Community Treasure Hunt

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

Start Hunting!