How to find f(x)*g(x)

Hi!
If i have f(x) = 1 + x and g(x) = 1 - x, i want to find h(x) = f(x)*g(x) = (1+x)*(1-x)=1-x.^2 What should i do?
Thanks!

Respuestas (2)

Star Strider
Star Strider el 25 de Mzo. de 2015

0 votos

It looks like you already did it. To code it using anonymous functions:
f = @(x) 1 + x;
g = @(x) 1 - x;
h = @(x) f(x).*g(x);
x = linspace(-10, 10);
figure(1)
plot(x, f(x), x, g(x), x, h(x))
grid
legend('f(x)', 'g(x)', 'h(x)', 'Location', 'south')

11 comentarios

Tran
Tran el 25 de Mzo. de 2015
Thank you Star Strider! But i want to receive and to show function h(x) = 1 - x.^2.
Star Strider
Star Strider el 25 de Mzo. de 2015
My pleasure!
Sorry, I misunderstood.
What you want would be:
h = @(x) 1 - x.^2;
The dot before the (^), i.e. (.^) ,denotes an element-wise operation, so x can be a scalar, vector or matrix and the function will still work.
James Tursa
James Tursa el 25 de Mzo. de 2015
What are you starting with? Strings for f(x) and g(x)? Or what?
Tran
Tran el 25 de Mzo. de 2015
Editada: Tran el 25 de Mzo. de 2015
I am starting with strings for f(x) and g(x), so i want to receive string h(x) = f(x)*g(x) = 1-x.^2. I have done it easy in Mathematica, but in Matlab i don't know what should i do? Thanks!
James Tursa
James Tursa el 25 de Mzo. de 2015
Editada: James Tursa el 25 de Mzo. de 2015
What do you want to end up with for h? Another string? Or a symbolic expression? Or a function handle using the dot operators (.* , ./ , .^ )?
Tran
Tran el 25 de Mzo. de 2015
Editada: Tran el 25 de Mzo. de 2015
Hi James ! I want to receive a symbolic expression h(x) = 1 - x.^2 , because i don't need calculate the expression, i only want to know components in the expression.
Star Strider
Star Strider el 25 de Mzo. de 2015
In that instance, use the Symbolic Math Toolbox.
Tran
Tran el 25 de Mzo. de 2015
Thank you so much Star Strider, i think this is what i want to find.
James Tursa
James Tursa el 25 de Mzo. de 2015
E.g.,
>> f = '1+x'
f =
1+x
>> g = '1-x'
g =
1-x
>> syms x
>> fx = eval(f)
fx =
x + 1
>> gx = eval(g)
gx =
1 - x
>> hx = simplify(fx*gx)
hx =
1 - x^2
Tran
Tran el 25 de Mzo. de 2015
Thank you so much James Tursa!
Star Strider
Star Strider el 25 de Mzo. de 2015
@James — Thank you!
@Tran — My pleasure!

Iniciar sesión para comentar.

Tanveer ul haq
Tanveer ul haq el 6 de Abr. de 2021

0 votos

syms x
f = 1 + x;
g = 1 - x;
h = simplify(f*g)

1 comentario

John D'Errico
John D'Errico el 6 de Abr. de 2021
Note that this does not actually solve the problem as asked.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mzo. de 2015

Comentada:

el 6 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by