three equations on one plot in matlab

To plot three equations on the same plot in Matlab, you can follow these steps:

  1. Define the equations you want to plot using symbolic variables. For example, let's say we want to plot the equations y1 = x^2, y2 = sin(x), and y3 = exp(x). We can define them as follows:
main.m
syms x
y1 = x^2;
y2 = sin(x);
y3 = exp(x);
43 chars
5 lines
  1. Create a range of x-values over which you want to plot the equations. For example, let's say we want to plot the equations for x ranging from -5 to 5 with a step size of 0.1. We can create the x-values as follows:
main.m
x = -5:0.1:5;
14 chars
2 lines
  1. Evaluate the equations at the x-values using the subs function. This will give us the y-values for each equation at each x-value. For example:
main.m
y1_values = subs(y1, x);
y2_values = subs(y2, x);
y3_values = subs(y3, x);
75 chars
4 lines
  1. Plot the equations using the plot function. For example, to plot y1, y2, and y3 on the same plot, you can use:
main.m
plot(x, y1_values, 'r', x, y2_values, 'g', x, y3_values, 'b');
63 chars
2 lines

Here, 'r', 'g', and 'b' represent the colors red, green, and blue, respectively.

  1. Add labels and a legend to the plot, if desired, using the xlabel, ylabel, and legend functions.
main.m
xlabel('x');
ylabel('y');
legend('y_1 = x^2', 'y_2 = sin(x)', 'y_3 = e^x');
76 chars
4 lines

Putting it all together, here's the complete code:

main.m
syms x
y1 = x^2;
y2 = sin(x);
y3 = exp(x);

x = -5:0.1:5;

y1_values = subs(y1, x);
y2_values = subs(y2, x);
y3_values = subs(y3, x);

plot(x, y1_values, 'r', x, y2_values, 'g', x, y3_values, 'b');
xlabel('x');
ylabel('y');
legend('y_1 = x^2', 'y_2 = sin(x)', 'y_3 = e^x');
274 chars
16 lines

This code will plot the three equations on the same plot in Matlab.

related categories

gistlibby LogSnag