Matlab smooth a 3d mesh plot - matlab

Hi I have a set of data A with each element corresponding to an x and y combination. When I plot this dat using mesh I get a graph with many spikes on it. This is not unnexpected but I would like a way to smooth these out to get a smooth surface.
I've tried to use the smooth3 command but cannot figure out how to make the suitable input.
Any help would be appreciated. Thanks
This is how my data is generated.
function v = f(x,y) % Not actual function
return x*rand()+y*rand()
end
x = 0.05:0.01:0.95;
y = 0.05:0.01:0.95;
o = zeros(length(x),length(y));
A = zeros(length(x), length(y));
for k = 1:5
for i = 1:length(x)
for j = 1:length(y)
o(i,j) = f([x(i), y(j)]);
end
end
A= A+o;
end
A = A/5;
This is what produces the plot.
[X,Y] = meshgrid(x);
mesh(A)

my be you can try a convolution of your variable A with a filter(the following is an example of a Gaussian filter).
C = conv2(A,fspecial('gaussian', hsize, sigma));
check conv2 and fspecial in matlab help

Related

Plotting the Lagrange interpolated curve on top of the equidistant points

I have made a simple code for Lagrange interpolation of vectors of data x and y and , but I need some help with plotting and displaying errors.
I wish to plot the equidistant points together with the Lagrange Interpolated curve in the same plot, and display the error of the approximation. Plotting the points is trivial, but I struggle with plotting the interpolated polynomial curve on top of the (x,y) plot.
clear
clc
x = -1:0.25:1;
y = 1./(1+25.*x.^2);
N = 1:length(y);
M = zeros(length(y),length(x));
for n = 1:length(x)
for i = 1:length(y)
l = 1;
index = find(N ~= i);
for jj = 1:length(index)
l = l.*(x(n)-y(index(jj)))./(y(i)-y(index(jj)));
end
M(i,n) = l;
end
end
plot(x,y)
All help is appreciated

Plot quiver polar coordinates

I want to plot the field distribution inside a circular structure with radius a.
What I expect to see are circular arrows that from the centre 0 go toward a in the radial direction like this
but I'm obtaining something far from this result. I wrote this
x_np = besselzero(n, p, 1); %toolbox from mathworks.com for the roots
R = 0.1:1:a; PHI = 0:pi/180:2*pi;
for r = 1:size(R,2)
for phi = 1:size(PHI,2)
u_R(r,phi) = -1/2*((besselj(n-1,x_np*R(1,r)/a)-besselj(n+1,x_np*R(1,r)/a))/a)*cos(n*PHI(1,phi));
u_PHI(r,phi) = n*(besselj(n,x_np*R(1,r)/a)/(x_np*R(1,r)))*sin(PHI(1,phi));
end
end
[X,Y] = meshgrid(R);
quiver(X,Y,u_R,u_PHI)
where u_R is supposed to be the radial component and u_PHI the angular component. Supposing the formulas that I'm writing are correct, do you think there is a problem with for cycles? Plus, since R and PHI are not with the same dimension (in this case R is 1x20 and PHI 1X361) I also get the error
The size of X must match the size of U or the number of columns of U.
that I hope to solve it if I figure out which is the problem with the cycles.
This is the plot that I get
The problem has to do with a difference in co-ordinate systems.
quiver expects inputs in a Cartesian co-ordinate system.
The rest of your code seems to be expressed in a polar co-ordinate system.
Here's a snippet that should do what you want. The initial parameters section is filled in with random values because I don't have besselzero or the other details of your problem.
% Define initial parameters
x_np = 3;
a = 1;
n = 1;
% Set up domain (Cartesian)
x = -a:0.1:a;
y = -a:0.1:a;
[X, Y] = meshgrid(x, y);
% Allocate output
U = zeros(size(X));
V = zeros(size(X));
% Loop over each point in domain
for ii = 1:length(x)
for jj = 1:length(y)
% Compute polar representation
r = norm([X(ii,jj), Y(ii,jj)]);
phi = atan2(Y(ii,jj), X(ii,jj));
% Compute polar unit vectors
rhat = [cos(phi); sin(phi)];
phihat = [-sin(phi); cos(phi)];
% Compute output (in polar co-ordinates)
u_R = -1/2*((besselj(n-1, x_np*r/a)-besselj(n+1, x_np*r/a))/a)*cos(n*phi);
u_PHI = n*(besselj(n, x_np*r/a)/(x_np*r))*sin(phi);
% Transform output to Cartesian co-ordinates
U(ii,jj) = u_R*rhat(1) + u_PHI*phihat(1);
V(ii,jj) = u_R*rhat(2) + u_PHI*phihat(2);
end
end
% Generate quiver plot
quiver(X, Y, U, V);

MATLAB - adding plot to another plot after loop, more sophisticatedly

i have a problem and i hope that i will find help there.
This is my example code. Its only part of my algorithm. For imagine, how the point are moving, during the equations, i need show contour of function with two variables and into the points. Becase i have more difficult function than parabolic function, so the equations are too long than i need. For this reason i move contour ploting before the loop. But i have problem. I need show countour always and points only for i-loop and my solution doesnt work. Please help me!
[R S] = meshgrid(-5:0.1:5, -5:0.1:5);
figure
contour(R, S, R.^2 + S.^2, 5);
axis([-5,5,-5,5])
axis square
hold on
for i=1:50
a = 0;
b = 1:2
B = repmat(b,5,1)
A = unifrnd(a,B)
x = A(1:5,1);
y = A(1:5,2);
scatter(x,y,'fill')
hold off
pause(0.5)
end
You should store the handle to your scatter plot and simply update the XData and YData properties of it rather than destroying the plot objects every time
[R S] = meshgrid(-5:0.1:5, -5:0.1:5);
figure
contour(R, S, R.^2 + S.^2, 5);
axis([-5,5,-5,5])
axis square
hold on
% Create a scatter plot and store the graphics handle so we can update later
hscatter = scatter(NaN, NaN, 'fill');
for i=1:50
a = 0;
b = 1:2
B = repmat(b,5,1)
A = unifrnd(a,B)
x = A(1:5,1);
y = A(1:5,2);
% Update the X and Y positions of the scatter plot
set(hscatter, 'XData', x, 'YData', y);
pause(0.5)
end

3D filled line plot

I have a matrix for contact positions and these positions are linear, therefore I can easily plot the contact positions within MATLAB and come out with the x amount of lines. At the moment I am plotting within a 2D graph.
for j= 1:5
for k= 1:20
Yijk(j,:,k)=x*tan_helix+one_array*(k-P)*Pb/P+one_array*(j-(L+1)/2)*Pb;
end
end
x_axis = linspace(0,b*1000, N+1);
figure;
for j=1:zPairs;
hold on
plot(x_axis,Yijk(j,:,k))
hold off
end
The above is only a small section of a large coding so all variables and parameters are stated else where.
Below is the graph this simply creates with a 2D graph:
What I wish to do is plot the correspoding contact to each of these positions, contact only occurs at positions > 0 and therefore will only occur along the lines plotted above. Therefore the plot will need to be in a 3D format and I am assuming that the lines will be plotted initially, then the contact_force and then a fill command as such - but I may be wrong.
What I am aiming to create is something similar to:
If any one has any guidance or tips it will be greatly appreciated as I am getting nowhere.
Please note the contact_force is also a matrix of the same dimensions as the contact positions.
for j = 1:zPairs
Xx = linspace(0,b*1000,N+1);
Yy = Yijk(j,:,1);
n = length(Xx);
Zz = contact_force(j,:,1);
Xp = zeros(2*n,1);
Yp = zeros(2*n,1);
Xp(1:N+1) = Xx;
Xp(N+2:2*(N+1)) = Xx(N+1:-1:1);
Yp(1:N+1) = Yy;
Yp(N+2:2*(N+1)) = Yy(N+1:-1:1);
Zp(1:N+1) = 0;
Zp(N+2:2*(N+1)) = Zz(N+1:-1:1);
figure(12);
hold on
patch(Xp,Yp,Zp,'c');
title('Zone of Contact');
hold off
end
The above code works great, but only creates one graph as it is for (j,:,1). I would like to change this so as that it is for (j,:,k) and k number of graphs are created. How would I set up this for loop ?
I wrote a little MATLAB code to test it out. This program creates a polygon on top of a 2D line. fill3 or patch functions are what you are looking for.
a = 2;
b = 1;
X = 0:10;
Y = a*X + b;
n = length(X);
Z = rand(n,1)*2+1;
Xp = zeros(2*n,1);
Yp = zeros(2*n,1);
Xp(1:n) = X;
Xp(n+1:2*n) = X(n:-1:1);
Yp(1:n) = Y;
Yp(n+1:2*n) = Y(n:-1:1);
Zp(1:n) = 0;
Zp(n+1:2*n) = Z(n:-1:1);
fill3(Xp,Yp,Zp,'c');

Hatching area between two symbolic curves matlab

I need to shade the area between symbolic curves and the x axis.
syms x
j(1) = x^2
j(2) = x^3
j(3) = x^5
j(4) = x^6
for i = 1:4
subplot(2,2,i);
f(i) = ezplot(j(i),[0,6000]);
Hatch(f(i))
end
This gives me an error. After looking in the matlab documentation, i end up with codes like
f1 := plot::Function2d(sqrt(x), x = 0..2, Color = RGB::Black):
Is this even matlab code? What's with the "::" and ":="? Why does this throw an error?
Thanks for any help guys!
Thanks!
The line f1 := plot::Function2d(sqrt(x), x = 0..2, Color = RGB::Black): is for MuPad (Symbolic Math toolbox). However, you can evaluate the symbolic functions without this toolbox using Matlab's ezplot.
The following figure
is given by (please see the comments that made your code work)
f{1} = 'x^2'; % declare as cell array {} of string ''
f{2} = 'x^3';
f{3} = 'x^5';
f{4} = 'x^6';
figure('Color', 'w');
for ii = 1:4 %do not use i or j in Matlab
subplot(2,2,ii);
h(ii) = ezplot(f{ii},[0,6000]); %the correct way to call ezplot
x = get(h(ii), 'XData'); %get the x and y data
y = get(h(ii), 'YData');
area(x,y,'FaceColor',[.7 0 0]); %plot the (x,y) area in red
end
Write you command under mupad and after to call it with Matlab command window have a look to this : MatLab and MuPad
For more information go here