Imposing a sine wave on line graph
12 views (last 30 days)
Show older comments
I am trying to impose a sine wave on a straight line graph and I want it to look like a sine wave with a rotated axis such that if I rotate the image I should see a sine wave and not some squint wave.
I am currently getting this (the squited wave which I don't want):
Here is my code:
clear
clc
close
x = linspace(0, 2*pi, 20);
y_ideal = 3*x;
a = 0.3;
x_ = linspace(0, 5*2*pi, 1000);
imposeFCN = a.*sin(x_*6);
rotated_by = -atan(3);
x_r = x_*cos(rotated_by)+ imposeFCN*sin(rotated_by);
y_r = -x_*sin(rotated_by) + imposeFCN*cos(rotated_by);
plot(x, y_ideal, x_r, y_r)
xlabel("x-axis")
ylabel("y-axis")
xlim([-pi 2*pi])
Thank you in advance
1 Comment
Answers (1)
Vinayak Gupta
on 29 May 2023
Edited: Vinayak Gupta
on 31 May 2023
Hi Joseph,
I just went through your code, and it is actually correct. A simplified version of mine is attached here.
x = linspace(0,10*pi,1000);
y = 0.3*sin(x*6);
r = 3;
theta = atan(r);
x_r = x*cos(theta)-y*sin(theta);
y_r = x*sin(theta)+y*cos(theta);
plot(x,r*x,x_r,y_r);
xlim([-pi/2 pi/2])
ylim([0 pi])
As you can see I added a ylim with same values as the xlim. The reason your line looks squint is not that its plotted incorrectly, but your axis are non uniform. I have used ylim, but you can also use "daspect" or "axis equal" to get expected results.
Refer to the following to get more information about "daspect" :
Refer to the following to get more information about the usage of "axis" :
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!