multiplying two functions and graphing the result in matlab

To multiply two functions and plot the result in MATLAB, follow these steps:

  1. Define the two functions f(x) and g(x).

    main.m
    syms x;
    f(x) = x^2 - 1;
    g(x) = cos(x);
    
    39 chars
    4 lines
  2. Multiply the functions together to get the resulting function.

    main.m
    h(x) = f(x) * g(x); 
    
    21 chars
    2 lines
  3. Create a vector of input values for the functions.

    main.m
    x_values = -10:0.1:10;
    
    23 chars
    2 lines
  4. Evaluate each function at each input value.

    main.m
    f_values = double(subs(f, x, x_values));
    g_values = double(subs(g, x, x_values));
    h_values = double(subs(h, x, x_values));
    
    123 chars
    4 lines
  5. Plot the original functions and the resulting function on the same graph.

    main.m
    plot(x_values, f_values, 'b', x_values, g_values, 'r', x_values, h_values, 'm');
    legend('f(x)', 'g(x)', 'h(x) = f(x) * g(x)');
    xlabel('x');
    ylabel('f(x), g(x), h(x)');
    title('Plot of f(x), g(x), and h(x)');
    
    207 chars
    6 lines

This will create a plot with three curves: one for f(x), one for g(x), and one for h(x) = f(x) * g(x).

Note: The double() function is used to convert the symbolic expression into a numerical array that can be plotted.

related categories

gistlibby LogSnag