Plotting a 3d figure in Matlab given a table - matlab

The table I have is like this:
An image of the table
X↓ Y->
Y = 0
Y = 1
Y = 2
Y = 3
X = 3
PXY = 4/54
PXY = 3/54
PXY = 2/54
PXY = 1/54
X = 5
PXY = 6/54
PXY = 5/54
PXY = 4/54
PXY = 3/54
X = 7
PXY = 8/54
PXY = 7/54
PXY = 6/54
PXY = 5/54
PXY is given by (x - y + 1)/54
How should I go about graphing this on MATLAB? I wish to draw a 3d histogram, something like this:
.
Most of my attempts have ended in errors usually because the X and Y vectors are not the same length.
If there's ways to do this Python as well, then please let me know, that works too.

In Matlab, you can use histogram2 specifying the edges of the bins in the x and y direction and the bin count.
% Your data
x = [3; 5; 7];
y = [0, 1, 2, 3];
p = (x - y + 1)/54;
% since x is a column vector and y a row vector, as of Matlab R2016b this results in a length(x)*length(y) matrix, equivalent to:
% p = zeros(length(x), length(y));
% for ix = 1:length(x)
% for iy = 1:length(y)
% p(ix, iy) = (x(ix) - y(iy) + 1)/54;
% end
% end
% Plot the histogram
histogram2('XBinEdges', [x; 9], ...
'YBinEdges', [y, 4], ...
'BinCounts', p);
xlabel('X')
ylabel('Y')
zlabel('P_{xy}')
Since you need 3 bins for x (4 bins for y), you need to provide 4 edges (5 edges for y), hence the additional points in the histogram2 call.
EDIT: on second thought, you probably want the bins centered on the x and y points that you provide, so perhaps you might want to use xEdges = [x - 1; x(end) + 1] and yEdges = [y - 0.5, y(end) + 0.5] as bin edges instead.

Related

I can not figure out why my for loop is not being taken in MATLAB

In MATLAB, I am trying to write a program that will take 3 coordinates on a graph, (x,y), use those values to solve a system of equations that will find the coefficients of a polynomial equation, y = ax^2 + bx + c, which I can then use to plot a parabola.
To test my code, I figured I could start with a polynomial, graph it, find the minimum location of the parabola, use its immediate neighbors for my other 2 locations, then run those 3 locations through my code which should spit out the coefficients of my original polynomial. But for some reason, my resulting parabola is right shifted and my values for b and c are incorrect.
Does anyone see where my issue is? I am out of ideas
clear all; close all;
x = -10:10;
%Original Polynomial
y = 6.*x.^2 + 11.*x -35;
% Find 3 Locations
[max_n, max_i] = min(y)
max_il = max_i - 1 % left neighbor of max_ni
max_nl = y(max_il) % value at max_il
max_ir = max_i + 1 % left neighbor of max_ni
max_nr = y(max_ir) % value at max_ir
% Solve for coefficients
syms a b c
equ = (a)*(max_i)^2 + (b)*(max_i) + (c) == (max_n);
equ_l = (a)*(max_il)^2 + (b)*(max_il) + (c) == (max_nl);
equ_r = (a)*(max_ir)^2 + (b)*(max_ir) + (c) == (max_nr);
sol = solve([equ, equ_l, equ_r],[a, b, c]);
Sola = sol.a
Solb = sol.b
Solc = sol.c
% New Polynomial
p = (sol.a).*(x).^2 + (sol.b).*(x) +(sol.c);
%Plot
plot(x,y); grid on; hold on;
plot(x, p);
axis([-10 10 -41 40])
[max_np, max_ip] = min(p)
legend('OG', 'New')
You are confusing the index into your array y, and the corresponding x coordinate.
x = -10:10;
y = 6.*x.^2 + 11.*x -35;
[max_n, max_i] = min(y)
Here. max_i is the index into the y array, the corresponding x coordinate would be x(max_i).
I suggest you find three data points to fit your curve to as follows:
[~, max_i] = min(y);
pts_x = x(max_i + (-1:1));
pts_y = y(max_i + (-1:1));
then use pts_x(i) and pts_y(i) as your x and y values:
syms a b c
equ = a * pts_x.^2 + b * pts_x + c == pts_y;
sol = solve(equ, [a, b, c]);

Making parametric plot with matlab

Is it possible to plot and make my x and y axis be depended on a parameter?
For example, I'd like that my x axis will be divided to 0 1/10L 2/10L 3/10L....L
and to plot the function on that exact axis, is it possible?
This is what I tried:
x = 0:0.1*L:10*L
plot(x,func1(x))
hold on
plot(x+xShift,func2(x)+yShift)
grid on
the shifts I'm adding are just some shifts because I'd like the second function to start from a different x and y.
This should do the trick:
L = 4;
xShift = 5;
yShift = 2;
y = #(x) x .^ 2;
x = 0:(0.1*L):(10*L);
x_shifted = x + xShift;
y = y(x);
y_shifted = y + yShift;
plot(x,y)
hold on;
plot(x_shifted,y_shifted)
hold off;
grid on;
ticks = 0:(10*L) + xShift;
set(gca,'XTick',ticks);
lim = max(y_shifted);
set(gca,'YLim',[0 max(y_shifted)]);
Output:

Random Points in an n-Dimensional Hypersphere

This Matlab code,
creates a set of random points defined by Cartesian coordinates and
uniformly distributed over the interior of an n-dimensional
hypersphere of radius r with center at the origin.
the source is here.
clear all
clc
m = 20000;
n = 2;
r = 2;
%// generate circle boundary
C = [3 4]; %// center [x y]
t = linspace(0, 2*pi, 100);
x = r*cos(t) + C(1);
y = r*sin(t) + C(2);
C_rep = repmat( C,m,1);
X = randn(m,n);
s2 = sum(X.^2,2);
X = X.*repmat(r*(rand(m,1).^(1/n))./sqrt(s2),1,n)+ C_rep;
%% Plot
figure(1), clf
plot(x,y,'b')
hold on
plot(C(1),C(2),'r.', 'MarkerSize', 50) % center point
hold on
plot(X(:,1),X(:,2),'g.','markersize',2);
axis equal;zoom off; zoom on;drawnow;shg;
ax = axis;
This is the output:
which is not what I want.
How to make the points distributed around a center point C?
When n = 2, 3, 4, k dimentions
What does s2 mean?

Interpolating xyz coordinates on MATLAB

clear
clc
A = [ --- spherical coordinates (theta, phi, r) [too long] --- ]; % size(A) is 1369 X 3
B = zeros(1369,3);
B(:,1) = A(:,3).*sind(A(:,1)).*cosd(A(:,2));
B(:,2) = A(:,3).*sind(A(:,1)).*sind(A(:,2));
B(:,3) = A(:,3).*cosd(A(:,1)); % converted to cartesian coordinates
raw_x = (B(:, 1))';
raw_y = (B(:, 2));
raw_z = (B(:, 3));
[X, Y] = meshgrid(raw_x, raw_y); % to use interp2
inter_z = vec2mat((raw_z)', 37);
Z = (inter_z)';
Above I am trying to interpolate and plot a set of spherical coordinates. For interpolation, I need to use below command
interp2(X,Y,Z,xi,yi,'bicubic') % xi and yi will be defined arbitrarily
However, X, Y and Z need to have the same size to use this command. I have size(X) = size(Y) = 1369 X 1369 and size(Z) = 37 X 37. I could not figure out how I can overcome this confusion correctly. Could you help out?

Using contour to plot function

I try to use contour to plot this function
3y + y^3 - x^3 = 5
I try contour(3*y+y^3-x^3-5) but it doesn't work.
How can I use contour to plot this function?
Are x and y properly defined as 2x2 matrices? If so then the "power" operator needs to be done on a component-wise basis (.^3 instead of ^3).
This works:
[x,y] = meshgrid(-2:.2:2,-2:.2:2);
contour(3*y+y.^3-x.^3-5)
Maybe you can try fcontour, which plots the contour lines of the function z = f(x,y) for constant levels of z over the default interval [-5 5] for x and y.
f = #(x,y) 3*y + y.^3 - x.^3 - 5;
fcontour(f)
Output:
I'm not convinced this addresses all parts of your question but it's a start. If you absolutely want contour to call a function, you can adjust my example to contour(X,Y,fh(X,Y)).
Better Approach
fh=#(x,y) 3*y + y.^3 - x.^3 -5; % <--- This is your function
x = (-4:.25:4)';
y = (-2:.25:2)';
[X,Y] = meshgrid(x,y);
Z = fh(X,Y);
contour(X,Y,fh(X,Y))
The Direct Approach (not preferred but works)
Notice the Z is transposed to make this work.
fh=#(x,y) 3*y + y.^3 - x.^3 -5; % <----this is your function
X = (-4:.25:4)';
Y = (-2:.25:2)';
Z = zeros(length(X),length(Y));
for i = 1:length(X)
for j = 1:length(Y)
xi = X(i);
yj = Y(j);
Z(i,j) = fh(xi,yj);
end
end
contour(X,Y,Z','LevelList',-60:10:60,'ShowText','on','LineWidth',1.4) % Fancied it up a bit