2017/09/30

Matlab: Plot multiple curves with legend

Example 1: Draw sin(x), sin(x/2) and sin(x)+sin(x/2)

>> x = 0:360;
>> y1 = sind(x);
>> y2 = sind(x/2);
>> y3 = y1+y2;
>> plot(x, y1, x, y2, x, y3);
>> legend('sin(x)','sin(x/2)','sin(x)+sin(x/2)')

Result:



Example 2: Draw log(x), log2(x), log10(x)

>> x = 0:0.01:5;
>> y1 = log(x);
>> y2 = log2(x);
>> y3 = log10(x);
>> plot(x, y1, x, y2, x, y3)
>> legend('log(x)','log2(x)','log10(x)')

Result:


Example 3: 3e-0.25*tsin(5t)

>> t = 0:0.01:30;
>> x1 = 3*exp(-0.25*t).*sin(5*t);
>> x2 = 0.2*cos(2*t);
>> x3 = 3*exp(-0.25*t).*sin(5*t)+0.2*cos(2*t);
>> plot(t,x1,t,x2,t,x3);
>> legend('x1','x2','x3');

Result:



Related Information:

Draw an Exponential Function with exp() with a custom Y-axis at x = 0
Exponential Function & Natural Logarithm 指數函數與自然對數

沒有留言:

張貼留言