How do I graph a vector with a variable on MATLAB - matlab

How can I graph something that looks like this (3x^2+5,4x) , I usually use GeoGebra to graph this but I can't put a domain it should look like this (GeoGebra)

From the graph, you seem to be taking 3x²+5 on the horizontal axis and 4x on the vertical axis and hence:
x = -5.5: 0.01: 5.5;
plot(3*x.^2+5, 4*x, 'r');
%other minor adjustments to match with the graph in your post
grid on; grid minor; %to display grid lines
set(gca, 'XAxisLocation', 'origin'); %setting x-axis location to 'origin'
legend('eq2'); %to display the legend
which gives:

Something like this should do it:
y = -10:0.1:10;
x = 3 * y.^2 + 5.4 * x;
plot(x, y)
grid on

Related

How to customize contour lines in Matlab?

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:

How to format graph so that border starts at max x and y and how to replace a plot command

I am trying to format my graph so that the border ends at the max x and max y so there is not extra space between them and the border. Also, I'm trying to completely replace my first plot command with my second one. Should I just delete my first plot? Currently, the second plot goes over my first plot, removing most of my formatting.
clear all, close all
%%command to clear all variables and log history
x = linspace(-pi, pi, 200);
%creating x variable between - pi and 200 with pi distance between each
%value
y = sin(x) + cos(x);
%Creating y varable with the help of x
figure
plot(x,y)
title('2-D Plot')
xlabel('Theta (in radians)')
ylabel('Function Y')
xlim([-pi pi])
ylim([-1.414 1.414])
grid
plot(x,y,'r--')
grid
To fit the axes box tightly around the data without manually adjusting the axis limits, use:
axis tight;
and instead of re-plotting, you can update the relevant properties of the line.
x = linspace(-pi, pi, 200);
y = sin(x) + cos(x);
figure;
h = plot(x,y); %handle for the line plot
title('2-D Plot');
xlabel('Theta (in radians)');
ylabel('Function Y');
grid;
axis tight; %to set the axis limits equal to the range of the data
set(h, 'LineStyle', '--', 'Color', 'r'); %Updating the plot with required changes

Computing the surface between two lines

I have the following figure, where I plotted two surfaces and I wanted to indicate the intersection of both of them. To do that, I did the following:
zdiff = z1-z2;
C = contours(x,y,zdiff,[0 0]);
xL = C(1, 2:end);
yL = C(2, 2:end);
zL = interp2(x, y, z1, xL, yL);
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 2,'Linestyle','--'); hold on;
line(xL, yL, zeros(size(zL)), 'Color', 'k', 'LineWidth', 2); hold off;
Now, I want to plot the vertical surface between the actual intersection (dash line) and its projection over XY (solid line), but I cannot figure out how to do that. Any ideas?
Another really simple option:
dist = (diff(xL).^2+diff(yL).^2).^0.5; %distance between x,y
cdist = [0, cumsum(dist)]; %cumsum of the distance
area = trapz(cdist,zL); %The area
Why not calculating it manually?
Something like (untested):
Area = 0
for i=1:numel(xL)-1
base = sqrt( (xL(i)-xL(i+1))^2 + (yL(i)-yL(i+1))^2);
Area =Area + base * (zL(i) + zL(i+1))/2;
end;
maybe not pretty but its a oneliner it might do the trick. maybe you have to adjust the format as this code is for (1,N) vectors
xL=(1:100); %size 1 100
yL=(1:100) ;%size 1 100
zL=rand(1,100);%size 1 100
line(xL,yL,zL)
line(xL,yL,zeros(size(zL)))
hold on
surf(repmat(xL,100,1),repmat(yL,100,1),cell2mat(arrayfun(#(x,y) linspace(x,y,100)',zL,zeros(size(zL)),'UniformOutput',false)))
xL=sin((1:30)/10); % Data generation for test only. Use your data
yL=cos((1:30)/10); % Data generation for test only. Use your data
zL=2+xL.*yL; % Data generation for test only. Use your data
surf([xL;xL],[yL;yL],[zeros(size(zL));zL]); % plot the surface

Double x and y-axis on imagesc and surf plot in Matlab

I want to put two values on the x-axis and y-axis of a imagesc and surf plot. Both plots plot the same values but the first is 2D and the second 3D.
The arrays that I want to put on the x-axis and y-axis have the same length and are both interesting to display in a single plot because they are related to each other.
So the x-axis and y-axis should look like the example solution in this post (matlab multiple x axis one below another).
In case of a imagesc plot a double x-axis is not that difficult with the code of johan of the example solution and some random data.
Z = rand(20,30);
Y = 32.*(1:size(Z,1));
X = 1:size(Z,2);
scale_factor_xaxis=10;
scale_factor_yaxis=100;
figure(1)
imagesc(X,Y,Z)
set(gca,'XDir','normal','YDir','normal');
title('title')
xlabel('first x label')
ylabel('first y label')
first_axis = gca;
sqzx = 0.15; %// distance to squeeze the first x-axis plot
sqzy = 0.15; %// distance to squeeze the first y-axis plot
set(first_axis, 'Position', get(first_axis, 'Position') + [0 sqzx 0 -sqzx ]);
ax2 = axes('Position', get(first_axis, 'Position') .* [1 1 1 1e-12] - [0 sqzx 0 0],'Color','none');
xlim(get(first_axis, 'XLim') * scale_factor_xaxis);
set(ax2, 'XScale', get(first_axis, 'XScale')); %// make logarithmic if first axis is too
xlabel('second x label')
The next step should be to put also a second scale and label to the y-axis. But I this won't work with the next part of the code:
ax2 = axes('Position', get(first_axis, 'Position') .* [1 1 1 1e-12] - [0 sqzy 0 0],'Color','r');
ylim(get(first_axis, 'YLim') * scale_factor_yaxis);
set(ax2, 'YScale', get(first_axis, 'YScale')); %// make logarithmic if first axis is too
ylabel('second y label')
With this the second y-label is plotted next to the second x-axis and the second y-scale is a x-scale. But I do not understand the code enough to get this the way I describe.

matlab plot image as background to graph

I have an image that I would like to set as the background of plot I am making. However it plots it so that the image taks up the axis([0 1000 0 1000]) while the axis for my graph is much smaller: ([24.5 24.6 67 67.1]). How do I align it so that the image is on the same scale as the graph?
I am performing the following commands:
h = figure;
hold on
voronoi(lats,longs);
I=imread('my_fig.png');
hi = imagesc(I);
set(hi,'alphadata',.5);
You can just call image with the right x and y vectors, like so (assuming your data is in x and y):
xImg = linspace(min(x), max(x), size(I, 2));
yImg = linspace(min(y), max(y), size(I, 1));
image(xImg, yImg, I, 'CDataMapping', 'scaled');
hold on;
plot(x, y);
In other words, generate a vector for each of the dimensions of the image that has the same number of points as that dimension of the image but goes between the range of your data.