-1

How can I fix this so that it would show all three plots in one plot?

I followed the instruction in this SO answer but didn't work out: https://stackoverflow.com/a/8773571/2414957

figure;
t = -pi:0.01:pi;

a = sin(t);
plot(t, a, 'r', 'DisplayName', 'a'); hold on;
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
plot(t, fhat, 'c', 'DisplayName', 'fhat');
hold on; 
p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
plot(t, p, 'b', 'DisplayName', 'p');
hold on;
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');

enter image description here

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408

2 Answers2

3

The 3 plots have variant range, therefore, you need to make normalization to plot all functions on the same space

figure;
t = -pi:0.01:pi;
a = sin(t);p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
%display
plot(t, p/norm(p), 'b', 'DisplayName', 'p');
hold on; %you need only one 'hold on'
plot(t, a/norm(a), 'r', 'DisplayName', 'a');
plot(t, fhat/norm(fhat), 'c', 'DisplayName', 'fhat');
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');

enter image description here

Mohammad nagdawi
  • 553
  • 4
  • 18
1

You are plotting all three functions. What happens is that p is drawn over a. They are both very small compared to that, and therefore fall on the same pixels on your screen.

To verify this you can zoom in:

set(gca,'ylim',[-1.5,1.5])

Alternatively, plot p using dots or dashes, so the other line shows through in between:

figure;
t = -pi:0.01:pi;
a = sin(t);
plot(t, a, 'r', 'DisplayName', 'a'); hold on;
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
plot(t, fhat, 'c', 'DisplayName', 'fhat');
hold on; 
p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
plot(t, p, 'b--', 'DisplayName', 'p'); % Note the 'b--' line format here!
hold on;
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120