-3

I tried to do my homework and i'm not sure if I did it right. I'm not entirely sure how linspace works and was hoping for some input on my code! I'll attach the assignment it's based on. [enter link description here][1] [enter image description here][2]

clc
clear all
figure(1);
figure(1);
x = 0:0.1:10
y1 = exp(-0.5*x).*sin(2*x)
y2 = exp(-0.5*x).*cos(2*x)
plot(x,y1,'-b',x,y2,'--r')
legend('y1','y2');
grid on
xlabel('\bfx axis');
ylabel('\bfy axis');
title('Sin and Cos Functions')

figure(2);
subplot(2,2,1)
plot(x,y1,'-b');
legend('y1','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy1')
title('Subplot of x and y1')

figure(3)
subplot(2,2,2)
plot(x,y2,'--r');
legend('y2','Location','northeast');
grid on
xlabel('\bfx')
ylabel('\bfy2')
title('Subplot of x and y2')

figure(4)
x = linspace(-2,8,200);
fun=(x.^2-6*x+5)./(x-3)
plot(x,fun,'b-','LineWidth',2)
axis([-2 8 -10 10]);
hold on;
title('Plot of f(x)')
xlabel('\bfx')
ylabel('\bfy')

(50 Points) Plot the function y(x) = (e^-0.5x)(Sin2x) for 100 values of x between 0 and 10. Use solid blue line for this function. Then plot the function y(x) = (e^-0.5x)(Cos2x)on the same axis. Use dashed red line for this function. Be sure to include a legend, title, axis labels and grid on the plots. Re-graph the two functions using subplot.

(50 Points) Plot the function Plot the function f(x)= ((X^2)-6x-5)/(x-3) using 200 points over the range −2 ≤ x ≤ 8. Note that there is an asymptote at x = 3, so the function will tent to infinity near to that point. In order to see the rest of the plot properly, you will need to limit the y-axis to the range -10 to 10.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • The first link is not reachable. Please post text as text and not as image – Thomas Sablik Oct 04 '20 at 19:13
  • Hello, thank you! I typed out the code instead since the link didn't work. This is my first time using matlab and had trouble saving the file. – Jessica Koval Oct 05 '20 at 00:51
  • Please read [ask]. Stack Overflow works best for specific questions. General feedback is certainly not in scope. It is just impossible to say if you did things right, we don’t know what you learned in your lectures, or what functionality you’re expected to use. On the other hand, I’m sure you can tell if the plot you created makes sense given the function you were supposed to plot, no? – Cris Luengo Oct 05 '20 at 01:06
  • You did some mistakes. `0:0.1:10` has 101 values, not 100. You are calling `figure(1)` twice. I think you have to create two subplots for the two graphs. – Thomas Sablik Oct 05 '20 at 06:17

1 Answers1

1

linspace takes 3 arguments: a starting value, an ending value, and n = the number of points you want to generate in between those two numbers. It outputs an array of n evenly spaced numbers between your starting and ending values. For example linspace(0, 10, 5) returns an array of 5 evenly spaced numbers starting with 0 and ending with 10.

Here's the code for figure 1

x = linspace(0, 10, 100);
y1 = exp(-0.5*x).*sin(2*x);
y2 = exp(-0.5*x).*cos(2*x);

figure(1)
plot(x, y1, '-b', x, y2, '--r')
title('This is the title')
legend('This is Y1', 'This is Y2')
xlabel('These are the X values')
ylabel('These are the Y values')
grid on

Figure 2. Both subplots can be under one figure handle (i.e. figure(2)).

figure(2)
subplot(2, 1, 1)
plot(x, y1, '-b')
title('This is the first subplot')
xlabel('X axis')
ylabel('Y axis')
grid on
subplot(2, 1, 2)
plot(x, y2, '--r')
title('This is the second subplot')
xlabel('Another X axis')
ylabel('Another Y axis')
grid on

Figure 3. Your code looks right. Here's a similar solution:

x2 = linspace(-2, 8, 200);
y3 = ((x2.^2)-6.*x2-5)./(x2-3);

figure(3)
plot(x2, y3, '-g')
title('The third figure')
grid on
ylim([-10 10])
xlabel('Another axis')
ylabel('The last axis')
Scott
  • 153
  • 7
  • 2
    _"you must call plot separately for each function."_ That's not true. You can plot multiple graphs with one call of `plot`. You don't need `hold on` and `hold off` for this simple task. – Thomas Sablik Oct 05 '20 at 08:14
  • I stand corrected. Edited. – Scott Oct 05 '20 at 16:58