Ibrahim Steel
Ibrahim Steel

Reputation: 23

Misaligned left and right y axes in Matlab

I am trying to produce two sinusoidal curves onto the same graph. the two have different amplitudes, so i have a left and right y axes. however, when i have a phase difference of Pi/4, the zero points of the two y axes are not aligned, instead being skewed differently. is there any way i can prevent this? thank you.

code:

function[] = improvised (a,b,c,d);
% a is time period, b is amplitude red, c amplitude blue
% d is phase shift blue relative to red
x = (0.0:0.001*a:2*a);
y = b*sin (2*pi*x/a);
z = c*sin ((2*pi*x/a)+(2*pi*d/a));
plot (x,y,'r-');
ax = gca;
ax.XAxisLocation = 'origin';
yyaxis left
xlabel ('blabla')
ylabel('blabla')

yyaxis right
plot (x,z,'b-');
ylabel('blabla')

on the right, the x axis crosses between 0.1 and 0, for seemingly no reason

Upvotes: 2

Views: 2466

Answers (1)

user6764549
user6764549

Reputation:

For the particular example that you have given, we can determine the amplitudes of the two curves programmatically. So it is possible to choose the y-axis limits of the two curves individually using the ylim() function as follows:

function[] = improvised (a,b,c,d)
% a is time period, b is amplitude red, c amplitude blue
% d is phase shift blue relative to red
x = (0.0:0.001*a:2*a);
y = b*sin (2*pi*x/a);
z = c*sin ((2*pi*x/a)+(2*pi*d/a));
plot (x,y,'r-');
ax = gca;
ax.XAxisLocation = 'origin';
yyaxis left
xlabel ('blabla')
ylabel('blabla')
ylim([-b,b])

yyaxis right
plot (x,z,'b-');
ylabel('blabla')
ylim([-c,c])

Now when we call the function with the given parameters:

>> improvised(.009, 6, 0.275, 0.001125 )

we get the following plot. enter image description here

Upvotes: 1

Related Questions