If I have a curve plotted in an axis is there a way of copying the curve and plotting next to it without running the function.
I.e. I have a code to plot a function from -1 to 0, I dont want the function to be correct from 0 to 1, I simply want to replicate the curve next to it.
Sorry for vague question but Im at a loss here
Using the function sin(x) as an example, you could do this
x_min = -1;
x_max = 0;
x = x_min:0.1:x_max;
y = sin(x);
hold on
plot(x, y, 'LineWidth', 1.2)
plot(x + x_max - x_min, y, 'LineWidth', 1.2)
plot([x_max x_max], [sin(x_min) sin(x_max)], 'k--')
axis equal
to get the following
Related
I am preparing a contour map where I am supposed to highlight the contour line for a specific level. For Example, my contour line values are lying between -1 and 1 and I want to highlight the line corresponding to the value 0. I tried to do this using the following procedure,
[M,c]=contourf(longitude,latitude,delta',-1:0.2:1);
s=size(c.LevelList,2);
for i=1:s
if (c.LevelList(i)==0)
c.LevelWidth=2;
end;
end;
However, it does nothing to the contour map. Can anyone please help me with the appropriate procedure?
I would suggest simply using contour on your desired levels to highlight after the initial contourf, like so:
% Input.
x = linspace(-2*pi, 2*pi, 101);
y = x + pi;
[X, Y] = meshgrid(x, y);
Z = 0.5 * (sin(X) + cos(Y));
% Levels to plot with contourf.
levelsf = -1:0.2:1;
% Levels to highlight.
levels = [0 0.3];
figure(1);
hold on;
% Contourf all levels.
contourf(X, Y, Z, levelsf);
% Highlight levels with simple contour.
contour(X, Y, Z, levels, 'r', 'LineWidth', 2);
hold off;
For highlighting levels = [0 0.3], you'll get:
The plot function allows us to plot all markers with a constant size s.
figure;
x = -10 : 10;
y = x .^ 2;
s = 10;
plot(x, y, 'bo', 'MarkerSize', s);
Suppose instead we would like each marker to have some individual size sx. For example, sx = abs(x) + 1.
One way to achieve this would be to use a for loop.
figure;
x = -10 : 10;
y = x .^ 2;
sx = abs(x) + 1;
hold on;
for i = 1 : length(x)
plot(x(i), y(i), 'bo', 'MarkerSize', sx(i));
end
This works well enough for small number of x. However, what if x is larger? For example, x = -100 : 0.01 : 100.
This now takes significantly longer, while plot(x, y, 'bo', 'MarkerSize', 100) would still complete almost instantly. Ideally, we would be able to do something such as plot(x, y, 'bo', 'MarkerSize', sx) where sx is a vector of sizes with each entry in sx corresponding to an entry in x and y. Unfortunately, that would give an error of Value not a numeric scalar.
Is there an efficient way to plot markers where each marker have varying individual sizes?
What you're trying to do is possible using scatter instead of plot as follows:
scatter(x,y,abs(x)+1)
I'm trying to graph a solution obtained through the quadratic formula in Matlab. Since it's obtained by the quadratic formula, there are two parts: plus and minus. The graph should be a hyperbola. How can I place the upper part and the bottom part on the same graph?
There are different ways. Let's say you want to plot the solution of y^2 = x, that is y = ±sqrt(x):
You can plot the two parts with the same color using a plot once…
x = 0:0.1:10;
plot(x, sqrt(x), 'k', x, -sqrt(x), 'k')
…or twice:
x = 0:0.1:10;
plot(x, sqrt(x), 'k')
hold on
plot(x, -sqrt(x), 'k')
hold off
Or you can plot everything in one go like you might draw it with a pen:
x = [10:-0.1:0 0.1:0.1:10];
y = [-sqrt(10:-0.1:0) sqrt(0.1:0.1:10)];
plot(x, y)
I'm trying to animate this spiral using matlab / octave I want it to spiral up or down
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
plot3 (r.*sin(t), r.*cos(t), z);
I tried using a for loop to animate it but that just gives me a cone shape see code and image below
clear all, clc,clf,tic
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
for ii=1:length(r)
ii
plot3 (r.*sin(t(ii)), r.*cos(t(ii)), z);
hold on
%pause (.00001)
end
Image
You could also use the comet3() package, which animates the trajectory through the plot:
delay = 0.001 % seconds
figure
comet3(r.*sin(t), r.*cos(t), z, delay);
This animates a continuous trajectory which I prefer over a discrete sequence of *'s.
The one downside is that the version of comet and comet3 that shipped with Octave 3.6.4 are slow, regardless of the delay you use. But this can be overcome by using the following trick courtesy of andyras in this SO question:
% plot the first point to get started
h = plot3(x(1),y(1),z(1),"b");
axis([min(x), max(x), min(y), max(y), min(z), max(z)]);
% refresh the plot in a loop through the rest of the data
for k = 1:length(z);
set(h, 'XData', x(1:k));
set(h, 'YData', y(1:k));
set(h, 'ZData', z(1:k));
pause (0.001); % delay in seconds
% alternatively could provide a velocity function
% pause(sqrt(vx(k)^2+vy(k)^2+vz(k)^2));
endfor
Minor note: once you've modified the function, you'll need to force Octave to reload it as it won't do this by default. You can either restart, or better yet use clear comet and clear comet3. Then the next time those functions are called, their definitions will be refreshed.
The following appears to work in Octave 3.6.2
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
figure
axis([-1 1 -1 1 0 1])
hold on
for ii=1:length(r)
plot3 (r(ii)*sin(t(ii)), r(ii)*cos(t(ii)), z(ii),'*');
pause (.001)
end
Certainly not the prettiest, but these are the first changes you need to make to your code for it to do something close to what you want.
t = 0:0.1:10*pi;
z = linspace (1, 0, numel (t));
for ii=1:length(t)
plot3 (z(ii)*sin(t(ii)),z(ii)*cos(t(ii)), z(ii));
hold on
pause (.00001)
end
This is not the trajectory solution, but here is a spinning tornado.
phi = linspace(0, 10*pi, 300);
r = linspace (0, 1, 300);
z = linspace (0, 1, 300);
s = 100; %speed of turning
for t = 0:0.01:10 %t is time
plot3 (r.*sin(phi+t*s), r.*cos(phi+t*s), z);
pause(0.01)
end
Can somebody tell me what I am doing wrong with the quiver plotting function when I don't really get arrows, it just fills the empty space with lots of blue.Look at the image below and then look at my code.
This is just a part of my contour since this eats up proccessing power if I try to draw it larger. But my function, the contours and everything else works, it's just the quiver I'm having trouble with.
interval = -100:100;
[X Y] = meshgrid(interval, interval);
h = figure;
contour(X, Y, Z);
hold on;
[FX,FY] = gradient(-Z);
quiver(X, Y, FX, FY);
hold off;
If I make my matrix more sparse, e.g. with "interval = linspace(-800, 1600, 1200);" the result will look like this:
EDIT:
What I need are contour lines like that, but the arrows should flow with them. Right now these just look like dots, even if I zoom in further. If I zoom out the entire window will be blue.
Here is the script in its entirety if anyone wants to play with it to figure this out.
m1 = 1;
m2 = 0.4;
r1 = [1167 0 0];
r2 = [-467 0 0];
G = 9.82;
w = sqrt( G*(m1+m2) / norm(r1-r2)^3 );
interval = linspace(-800, 1600, 1200);
% Element-wise 2-norm
ewnorm = #(x,y) ( x.^2 + y.^2 ).^(1/2);
% Element-wise cross squared
ewcross2 = #(w,x,y) w^2.*( x.*x + y.*y );
[X Y] = meshgrid(interval, interval);
Z = - G*m1 ./ ewnorm( X-r1(1), Y-r1(2) ) - G*m2 ./ ewnorm( X-r2(1), Y-r2(2) ) - 1/2*ewcross2(w,X,Y);
h = figure;
contour(Z);
daspect([1 1 1]);
saveas(h, 'star1', 'eps');
hold on;
[FX,FY] = gradient(-Z);
quiver(X, Y, FX,FY);
hold off;
The problem is that the mesh is too dense. You only want to have as few elements as necessary to generate a useful mesh. As such, try reducing the density of the mesh:
interval = -100:2:100
If you're going to be changing the limits often, you probably want to avoid using the X:Y:Z formulation. Use the linspace function instead:
interval = linspace(-100,100,10);
This will ensure that no matter what your limits, your mesh will be 10x10. In the comment below, you mention that the arrows are appearing as dots when you use a very large mesh. This is to be expected. The arrows reflect "velocity" at a given point. When your plot is scaled out to a very large degree, then the velocity at any given point on the plot will be almost 0, hence the very small arrows. Check out the quiver plot documentation, as well as the quivergroup properties, to see more details.
If you absolutely must see arrows at a large scale, you can try setting the AutoScale property to off, or increasing the AutoScaleFactor:
quiver(X, Y, FX, FY, 'AutoScale', 'off');
quiver(X, Y, FX, FY, 'AutoScaleFactor', 10);
You may also want to play with the MarkerSize and MaxHeadSize properties. I really just suggest looking at all the QuiverGroup properties and trying things out.
You could use a threshold
interval = -100:100;
[X Y] = meshgrid(interval, interval);
h = figure;
contour(X, Y, Z);
hold on;
[FX,FY] = gradient(-Z);
GM = sqrt(FX.^2 + FY.^2);
threshold = 0.1;
mask = GM > threshold;
quiver(X(mask), Y(mask), FX(mask), FY(mask));
hold off;
This will show only vectors with a magnitude > 0.1;