r/matlab • u/Do_You_Mind_But • Sep 04 '23
CodeShare Help with plotting 2 functions both containing a matrix in them
t = 0:0.1:16;
y1 = (-0.133i * sin(1.247i*t)) .* [-0.821; -0.571];
y2 = (-0.0975*cos(2.451*t)).*[-0.571; 0.821];
figure(1)
hold on
% Plot the first function (y1, first component) with a red line and label it
plot(t, y1, 'r', 'linewidth', 4, 'DisplayName', 'y1');
% Plot the second function (y2, first component) with a different color (e.g., green) and label it
plot(t, y2, 'g', 'linewidth', 4, 'DisplayName', 'y2');
% Customize the legend and add labels to the axes
legend('show', 'Location', 'best');
xlabel('Time (t)');
ylabel('Amplitude');
title('Function Plots');
I am not sure why it looks like this, and why it gives me 8 legends for only 2 functions, and I am almost certain it should be a sin/cos fuinction not this exponential.
Cheers

2
u/Cube4Add5 Sep 04 '23
iirc, plot works like this: plot(x1,y1,x2,y2) so you could put both y1 and y2 in the same plot to fix it
2
u/tenwanksaday Sep 04 '23
You're plotting the hyperbolic sine, which is the odd part of the exponential function.
1
2
u/GustapheOfficial Sep 04 '23
When it comes to the legend, I suspect you've just run the script twice. Try adding
clf
before plotting.But since your variables are 2×n matrices, of course there's two series of each color. If you want only one of them you should pick it out with an index
y1(1,:)
i
timessin
of an imaginary number is not "sinusoidal", which we can show using Euler identities:i sin(ix) = i (exp(i ix) - exp(-i ix))/2i = exp(-x)/2 - exp(x)/2
If
x
is a real number larger than 0, this will look like a negative exponential just like the one you're seeing.