What is the simplest way for ploting a TSP graph in matlab? - matlab

I have created a very simple Traveling Salesman Problem`s algorithm and now I need to output the results in a visual garph.
I have a matrix with path weights and an correctly ordered array of points (the path)
The Internet is full of very complex and detailed examples of TSP (like for DNA research) yet I am looking for some basic plotting function.

Not exactly sure what you want to plot, but assuming it is just the resulting path it is quite simple:
x=randperm(10); % Your x coordinates
y=randperm(10); % Your y coordinates
% Now plot these points and make sure you add 1 term to return to the starting point
plot([x x(1)],[y y(1)])

Related

Using matlab to obtain the vector fields and the angles made by the vector field on a closed curve?

Here is the given system I want to plot and obtain the vector field and the angles they make with the x axis. I want to find the index of a closed curve.
I know how to do this theoretically by choosing convenient points and see how the vector looks like at that point. Also I can always use
to compute the angles. However I am having trouble trying to code it. Please don't mark me down if the question is unclear. I am asking it the way I understand it. I am new to matlab. Can someone point me in the right direction please?
This is a pretty hard challenge for someone new to matlab, I would recommend taking on some smaller challenges first to get you used to matlab's conventions.
That said, Matlab is all about numerical solutions so, unless you want to go down the symbolic maths route (and in that case I would probably opt for Mathematica instead), your first task is to decide on the limits and granularity of your simulated space, then define them so you can apply your system of equations to it.
There are lots of ways of doing this - some more efficient - but for ease of understanding I propose this:
Define the axes individually first
xpts = -10:0.1:10;
ypts = -10:0.1:10;
tpts = 0:0.01:10;
The a:b:c syntax gives you the lower limit (a), the upper limit (c) and the spacing (b), so you'll get 201 points for the x. You could use the linspace notation if that suits you better, look it up by typing doc linspace into the matlab console.
Now you can create a grid of your coordinate points. You actually end up with three 3d matrices, one holding the x-coords of your space and the others holding the y and t. They look redundant, but it's worth it because you can use matrix operations on them.
[XX, YY, TT] = meshgrid(xpts, ypts, tpts);
From here on you can perform whatever operations you like on those matrices. So to compute x^2.y you could do
x2y = XX.^2 .* YY;
remembering that you'll get a 3d matrix out of it and all the slices in the third dimension (corresponding to t) will be the same.
Some notes
Matlab has a good builtin help system. You can type 'help functionname' to get a quick reminder in the console or 'doc functionname' to open the help browser for details and examples. They really are very good, they'll help enormously.
I used XX and YY because that's just my preference, but I avoid single-letter variable names as a general rule. You don't have to.
Matrix multiplication is the default so if you try to do XX*YY you won't get the answer you expect! To do element-wise multiplication use the .* operator instead. This will do a11 = b11*c11, a12 = b12*c12, ...
To raise each element of the matrix to a given power use .^rather than ^ for similar reasons. Likewise division.
You have to make sure your matrices are the correct size for your operations. To do elementwise operations on matrices they have to be the same size. To do matrix operations they have to follow the matrix rules on sizing, as will the output. You will find the size() function handy for debugging.
Plotting vector fields can be done with quiver. To plot the components separately you have more options: surf, contour and others. Look up the help docs and they will link to similar types. The plot family are mainly about lines so they aren't much help for fields without creative use of the markers, colours and alpha.
To plot the curve, or any other contour, you don't have to test the values of a matrix - it won't work well anyway because of the granularity - you can use the contour plot with specific contour values.
Solving systems of dynamic equations is completely possible, but you will be doing a numeric simulation and your results will again be subject to the granularity of your grid. If you have closed form solutions, like your phi expression, they may be easier to work with conceptually but harder to get working in matlab.
This kind of problem is tractable in matlab but it involves some non-basic uses which are pretty hard to follow until you've got your head round Matlab's syntax. I would advise to start with a 2d grid instead
[XX, YY] = meshgrid(xpts, ypts);
and compute some functions of that like x^2.y or x^2 - y^2. Get used to plotting them using quiver or plotting the coordinates separately in intensity maps or surfaces.

Matlab : Intersect point of curves

Say for example I have data which forms the parabolic curve y=x^2, and I want to read off the x value for a given y value. How do I go about doing this in MATLAB?
If it were a straight line, I could just use the equation of the line of best fit to calculate easily, however I can't do this with a curved line. If I can't find a solution, I'll solve for roots
Thanks in advance.
If all data are arrays (not analytical expressions), I usually do that finding minimal absolute error
x=some_array;
[~,ind]=min(abs(x.^2-y0))
Here y0 is a given y value
If your data are represented by a function, you can use fsolve:
function y = myfun(x)
y=x^2-y0
[x,fval] = fsolve(#myfun,x0,options)
For symbolic computations, one can use solve
syms x
solve(x^2 - y0)
Assuming your two curves are just two vectors of data, I would suggest you use Fast and Robust Curve Intersections from the File Exchange. See also these two similar questions: how to find intersection points when lines are created from an array and Finding where plots may cross with octave / matlab.

matlab: cdfplot of relative error

The figure shown above is the plot of cumulative distribution function (cdf) plot for relative error (attached together the code used to generate the plot). The relative error is defined as abs(measured-predicted)/(measured). May I know the possible error/interpretation as the plot is supposed to be a smooth curve.
X = load('measured.txt');
Xhat = load('predicted.txt');
idx = find(X>0);
x = X(idx);
xhat = Xhat(idx);
relativeError = abs(x-xhat)./(x);
cdfplot(relativeError);
The input data file is a 4x4 matrix with zeros on the diagonal and some unmeasured entries (represent with 0). Appreciate for your kind help. Thanks!
The plot should be a discontinuous one because you are using discrete data. You are not plotting an analytic function which has an explicit (or implicit) function that maps, say, x to y. Instead, all you have is at most 16 points that relates x and y.
The CDF only "grows" when new samples are counted; otherwise its value remains steady, just because there isn't any satisfying sample that could increase the "frequency".
You can check the example in Mathworks' `cdfplot1 documentation to understand the concept of "empirical cdf". Again, only when you observe a sample can you increase the cdf.
If you really want to "get" a smooth curve, either 1) add more points so that the discontinuous line looks smoother, or 2) find any statistical model of whatever you are working on, and plot the analytic function instead.

finding the intersection where multiple 3D parametric equations meet

I have some 3D parametric equations that I plot in matlab/octave and would like to find out where they intersect how can I go about doing this. Please note this is just a simple example I plan on having multiple parametric equations that will intersect.
What I'm trying to do is begin and end each plot at an intersection point.
My first thought was putting all the intersection points found at (t) into an array and plotting the ones I want I just couldn't figure out how to get the intersections at (t)
Example matlab/octave code below:
clear all, clf
t = 0:pi/60:2.45*pi;
plot3 ((t).*cos(t), (t).*sin(t), (t),'b*');
hold on
plot3 ((t).*sin((t)), (t).*cos((t)), (t),'r');
Here's an image with the arrows pointing to the intersecting points
I'd say that it is better to solve this system of equations analytically if possible.
Look, with your original code (just the plotted line styles were changed)
clear all, clf
t = 0:pi/60:2.45*pi;
plot3 ((t).*cos(t), (t).*sin(t), (t),'*b-');
hold on
plot3 ((t).*sin((t)), (t).*cos((t)), (t),'or-');
I have this picture at one of the intersections:
Its nice because the two plots intersect perfectly with each other in one of the plotted points.
Now consider the following situation. I changed your second line to
t = 0:0.05:2.45*pi;
Now I see this at the intersection point:
Now the two 3D polygonal lines generated for different values of t parameter in general case will not have any common points. So simple check for intersection of those linear segments will fail.
Same thing will happed if you update your equations to more complicated ones with more complicated dependencies over t (otherwise you are really lucky).
Analytical solution (if possible) will be unbeatable here.
As a numerical way to do this, consider distance D from an arbitrary point P1(x1(t1),y1(t1),z1(t1)) on one line to an arbitrary point P2(x2(t2),y2(t2),z2(t2)) on another one. Then find minimum of D as a function of two independent parameters t1 and t2. If D at minimum is zero (or very close to zero), then these two lines intersect. Otherwise they just pass each other.

plot a 1 x n vector in a two dimensional cartesian coordinate system

I have a 1 x 10 vector as shown below:
V = [0.500 -5.433 0.543 0.321 1.432 0.543 -0.576 -0.145 -1.322 -0.222]
and want to plot this on MATLAB using plot.
I used plot(v,0,'kx,'marker',10) but it does not seem reasonable for me. Any idea on how to go about with this?
Does anyone have a very good resource for for ISOMAP? Need a very comprehensive step by step easy tutorials on Isomaps. If i can have good videos on it will be very good.
you almost got it, just write:
plot(v,'kx','MarkerSize',10);
note, that I wrote plot and not Plot, Matlab is case sensitive...
when you only have a single vector the plot function assumes that for the x-axis it takes the number of elements of the vector, i.e. plot(1:numel(v),v,...). I recommend you to use the Matlab documentation, if you had read it, you'd see an example that could show you what you did wrong.
bla's solution is okay and produces a scatter plot.
However, you can make a line plot, etc.
Though, notice it is V and not v, as he pointed out that MATLAB is case-sensitive.
Here is how to generate a line plot:
x=0:length(V)
plot(x,V,'r--o',x,V,'r*')
Output: