Solved

rotate a hyperbola in Matlab - code provided

Posted on 2009-03-30
7
2,541 Views
Last Modified: 2016-02-10
http://www.experts-exchange.com/Programming/Languages/MatLab/Q_24269123.html

following on from the above;

I understand the concept better now thanks to the link provided

the function xfm1 rotates a point on the locus of the hyperbola about 60 degrees from P(x',y') to P(x",y") around (xo,yo).

but I am stuck on how xfm1 below is incorporated into create_hyperbola as in the code box below;

Can someone please help
% Program to plot the hyperbola
% y^2/a^2 - x^2/b^2 = 1
% The hyperbolae are open up/down, so that x is the independent variable
% for plotting. (Using the form x^2/a^2 - y^2/b^2 = 1 requires that y be
% the independent variable, which is awkward programming-wise.)
clear % all variables
figure(1), hold off % start a new figure
set(gca,'FontSize',14) % adjust fontsize
xmax = 30; ymax = 20;
x = linspace(-xmax,xmax,1001); % array of x values for plot : This function generates vector with 1001 points equally distributed between -xmax and xmax. 
                                %1001 is probably to have 500 points on one side (>0), 500 on another side (<0) and to include 0 itself. So you will have 
                                 %more natural numbers. 
 
a = 5; b = 3;
y=sqrt(((x.^2)./(b^2)+1).*a^2); % corresponding y values
plot(x,y)
hold on % add to current plot
plot(x,-y) % Plot other half of hyperbola
 
% plot the elipse
% y^2/a^2 - x^2/b^2 = 1
 
y=sqrt(((-x.^2)./(b^2)+1).*a^2); % corresponding y values
plot(x,y)
plot(x,-y) % Plot other half of elipse
 
axis([-xmax xmax -ymax ymax]) % specify axis limits
xlabel('x')
ylabel('y')
title(['Hyperbola and elipse $y^2/a^2 - x^2/b^2 = 1$;$y^2/a^2 + x^2/b^2 = 1$; $a$ = ', num2str(a), ...
', $b$ = ', num2str(b),';'],'Interpreter','latex')
% Add axes
plot([0 0],[-ymax ymax],'k') % y axis (black line - k)
plot([-xmax xmax],[0 0],'k') % x axis
 
f1=sqrt(a.^2+b.^2);
f2=-(sqrt(a.^2+b.^2));
plot([0 0],[f1 f2],'.')
text(0,f1,'\leftarrow +d','VerticalAlignment','middle','HorizontalAlignment','left')
text(0,f2,'\leftarrow -d','VerticalAlignment','middle','HorizontalAlignment','left')
 
f3=sqrt(a.^2-b.^2);
f4=-(sqrt(a.^2-b.^2));
plot([0 0],[f3 f4],'.')
text(0,f3,'\leftarrow +c','VerticalAlignment','middle','HorizontalAlignment','left')
text(0,f4,'\leftarrow -c','VerticalAlignment','middle','HorizontalAlignment','left')
 
text(0,a, '\leftarrow +a')
text(0,-a, '\leftarrow -a')
 
 
 
function [xout,yout] = xfm1(xin,yin,theta,x_offset,y_offset)
% Program to rotate and translate x,y values from x",y" to x,y space.
% Written to plot hyperbolas for time of arrival code.
% theta value assumed to be in radians.
 
%  rotation matrix
xfm = [cos(theta) sin(theta); ...
      -sin(theta) cos(theta)];
% make x,y values into a column vector
r_in = [xin; yin];
% rotate 
r_out = xfm*r_in;
x = r_out(1,:); y = r_out(2,:);
xout = x + x_offset; 
yout = y + y_offset;

Open in new window

0
Comment
Question by:jtiernan2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:yuk99
ID: 24025467
You cannot put function and script into one m-file. Function should be in a separate m-file with the same name as function name. So create xfm1.m file and paste your function code there. Keep this file in the same  directory with your script.
You can read more about m-files, scripts and functions here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html

I should add, that function can be in the same m-file with another function, is it's nested function. If you are interested in this, see here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-39683.html
0
 
LVL 1

Author Comment

by:jtiernan2008
ID: 24031504
thanks, ok so I just invoke the function as long as it is in the same folder.

but how do i import the rotated values?
do I use x = linspace(-xmax,xmax,1001); and y=sqrt(((x.^2)./(b^2)+1).*a^2);

how do I use the function xfm1(xin,yin,theta,x_offset,y_offset) with relation to the create_hyperbola function.

thats where I am getting confused
0
 
LVL 15

Expert Comment

by:yuk99
ID: 24032496
Add something like this to your code. x and y are coordinates for original hyperbola, you need to apply the function to every part of your figure.
When you call function in Matlab, input and output parameters names do not have to match those in function implementation.

BTW I think you made a wrong rotation matrix. No, it's correct, but rotate in not usual direction. It should be
xfm = [cos(theta) -sin(theta); ...
      sin(theta) cos(theta)];

theta=1; % in radians, 1 radian = 180/pi degrees
xoff=0;
yoff=0;
[xout,yout] = xfm1(x,y,theta,xoff,yoff);
plot(xout,yout)

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jtiernan2008
ID: 24033889
Ok, I added this at the end of code but the plot does not seem to be correct as per page 4 of the tutorial... am I doing this wrong?
% Program to plot the hyperbola
% y^2/a^2 - x^2/b^2 = 1
% The hyperbolae are open up/down, so that x is the independent variable
% for plotting. (Using the form x^2/a^2 - y^2/b^2 = 1 requires that y be
% the independent variable, which is awkward programming-wise.)
clear % all variables
figure(1), hold off % start a new figure
set(gca,'FontSize',14) % adjust fontsize
xmax = 30; ymax = 20;
x = linspace(-xmax,xmax,1001); % array of x values for plot : This function generates vector with 1001 points equally distributed between -xmax and xmax. 
                                %1001 is probably to have 500 points on one side (>0), 500 on another side (<0) and to include 0 itself. So you will have 
                                 %more natural numbers. 
 
a = 5; b = 3;
y=sqrt(((x.^2)./(b^2)+1).*a^2); % corresponding y values
plot(x,y)
hold on % add to current plot
plot(x,-y) % Plot other half of hyperbola
 
 
 
% plot the elipse
% y^2/a^2 - x^2/b^2 = 1
 
y=sqrt(((-x.^2)./(b^2)+1).*a^2); % corresponding y values
plot(x,y)
plot(x,-y) % Plot other half of elipse
 
axis([-xmax xmax -ymax ymax]) % specify axis limits
xlabel('x')
ylabel('y')
title(['Hyperbola and elipse $y^2/a^2 - x^2/b^2 = 1$;$y^2/a^2 + x^2/b^2 = 1$; $a$ = ', num2str(a), ...
', $b$ = ', num2str(b),';'],'Interpreter','latex')
% Add axes
plot([0 0],[-ymax ymax],'k') % y axis (black line - k)
plot([-xmax xmax],[0 0],'k') % x axis
 
f1=sqrt(a.^2+b.^2);
f2=-(sqrt(a.^2+b.^2));
plot([0 0],[f1 f2],'.')
text(0,f1,'\leftarrow +d','VerticalAlignment','middle','HorizontalAlignment','left')
text(0,f2,'\leftarrow -d','VerticalAlignment','middle','HorizontalAlignment','left')
x1=10;
 
y1=sqrt(((x1.^2)./(b^2)+1).*a^2);
line([0 x1], [f1 y1],'color','r')
line([0 x1], [f2 y1],'color','r')
 
f3=sqrt(a.^2-b.^2);
f4=-(sqrt(a.^2-b.^2));
plot([0 0],[f3 f4],'.')
text(0,f3,'\leftarrow +c','VerticalAlignment','middle','HorizontalAlignment','left')
text(0,f4,'\leftarrow -c','VerticalAlignment','middle','HorizontalAlignment','left')
 
text(0,a, '\leftarrow +a')
text(0,-a, '\leftarrow -a')
 
theta=1; % in radians, 1 radian = 180/pi degrees
xoff=0;
yoff=0;
[xout,yout] = xfm1(x,y,theta,xoff,yoff);
plot(xout,yout)

Open in new window

0
 
LVL 1

Author Comment

by:jtiernan2008
ID: 24033897
tutorial
toa.pdf
0
 
LVL 15

Accepted Solution

by:
yuk99 earned 500 total points
ID: 24034728
Of course, you didn't put the parameters you need.
Theta=60 deg, Xo=10, Yo=5.
Basically now before drawing anything you need to transform x and y coordinates with your function. Then plot new coordinates.
I didn't put focus points on the figure. Hope you can do it.
clear % all variables
figure(1), hold off % start a new figure
set(gca,'FontSize',14) % adjust fontsize
xmax = 30; ymax = 20;
x = linspace(-xmax,xmax,1001);
 
a = 5; b = 3;
theta=60*pi/180; % in radians, 1 radian = 180/pi degrees
xoff=10;
yoff=5;
y=sqrt(((x.^2)./(b^2)+1).*a^2); % corresponding y values
[xout,yout] = xfm1(x,y,theta,xoff,yoff); % rotation and shift
 
plot(xout,yout)
hold on % add to current plot
[xout,yout] = xfm1(x,-y,theta,xoff,yoff); % rotation and shift of negative part
plot(xout,yout) % Plot other half of hyperbola
 
axis([-xmax xmax -ymax ymax]) % specify axis limits
xlabel('x')
ylabel('y')
 
% Add axes
plot([0 0],[-ymax ymax],'k') % y axis
plot([-xmax xmax],[0 0],'k') % x axis
 

Open in new window

0
 
LVL 1

Author Closing Comment

by:jtiernan2008
ID: 31564583
Brilliant... yes I understand you invoke it through the plot function after defining the variables above...
I know how to get the focus points as well
thanks a million :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
static class 3 82
countClumps  challenge 10 214
object oriented javascript web form 8 171
Problem to Alipay 10 72
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question