Need help in plotting the 3 variable function's contour
7 views (last 30 days)
Show older comments
Yasir Iqbal
on 22 Jul 2022
Commented: Walter Roberson
on 25 Jul 2022
clc
clear all
close all
% format long
step = 10;
z = linspace(0,1.2e-6,step);
x = linspace(-0.1e-6,0.1e-6,step);
y=linspace(-0.1e-6,0.1e-6,step);
%r=sqrt(x.^2+y.^2);
z1 = 1e-6;
L = 9.995498999999999e-07;
a1 = 8.999974099901931e-10;
s = linspace(-L,L,1e5);
F0 = 3.15e7;
Mat_V = zeros(length(z),length(x),length(y));
for i = 1 : length(z)
i
for j = 1 : length(x)
for k= 1:length(y)
if abs(sqrt(x(j).^2+y(k).^2)) > sqrt((a1*z1)*(1-(z(i)*z(i)/z1/z1)))
F_f = s ./((sqrt(x(j).^2+y(k).^2) + (z(i)-s).^2).^0.5);
V_f = @(z,F_f) F0 * z1 / (z1 * log((L+z1)/(-L+z1)) - 2 * L) * (trapz(s,F_f)) - F0 * z;
Mat_V(i,j,k) = V_f(z(i),F_f);
else
Mat_V(i,j,k) = 0;
end
end
end
end
[X,Y,Z] = ndgrid(x,y,z) ;
F =Mat_V(Z,X,Y) ;
figure
hold on
for i = 1:100
surf(X(:,:,i),Y(:,:,i),Z(:,:,i),F(:,:,i)) ;
end
axis([-3 3 -3 3]);grid minor
view(3);
shading interp
%set(gca,'FontSize',25,'fontweight','bold')
%size = 35;
%xlabel('X ({\mu}m)','FontSize',size,'fontweight','bold')
%ylabel('Z ({\mu}m)','FontSize',size,'fontweight','bold')
%axis equal
2 Comments
Walter Roberson
on 22 Jul 2022
Mat_V(i,j,k) = V_f(z(i),F_f);
That is an array assignment with integer indices.
F =Mat_V(Z,X,Y) ;
That is an attempt to index the array with non-integer indices. Perhaps you want to use permute()?
Accepted Answer
Walter Roberson
on 23 Jul 2022
format long g
step = 10;
z = linspace(0,1.2e-6,step);
x = linspace(-0.1e-6,0.1e-6,step);
y=linspace(-0.1e-6,0.1e-6,step);
%r=sqrt(x.^2+y.^2);
z1 = 1e-6;
L = 9.995498999999999e-07;
a1 = 8.999974099901931e-10;
s = linspace(-L,L,1e5);
F0 = 3.15e7;
Mat_V = zeros(length(z),length(x),length(y));
for i = 1 : length(z)
i
for j = 1 : length(x)
for k= 1:length(y)
if abs(sqrt(x(j).^2+y(k).^2)) > sqrt((a1*z1)*(1-(z(i)*z(i)/z1/z1)))
F_f = s ./((sqrt(x(j).^2+y(k).^2) + (z(i)-s).^2).^0.5);
V_f = @(z,F_f) F0 * z1 / (z1 * log((L+z1)/(-L+z1)) - 2 * L) * (trapz(s,F_f)) - F0 * z;
Mat_V(i,j,k) = V_f(z(i),F_f);
else
Mat_V(i,j,k) = 0;
end
end
end
end
[X,Y,Z] = ndgrid(x,y,z) ;
F = permute(Mat_V, [2 3 1]); %created as z x y
figure
hold on
for i = 1:size(F,3)
surf(X(:,:,i),Y(:,:,i),Z(:,:,i),F(:,:,i)) ;
end
xlim auto; ylim auto; grid minor
view(3);
shading interp
5 Comments
Walter Roberson
on 23 Jul 2022
I modified the code to be slightly more efficient.
You will notice that the outputs are essentially flat. If you look at the term that I write into the temporary variable "temp" and save overall into the array Temp, you will find that that term is at most 6E-14 and so the subtraction of F0*z(i) that you were doing is contributing all of the significant value to the Mat_V result -- to withing round-off error for plotting purposes, your calculations just produce flat sheets at different z levels.
format long g
step = 10;
z = linspace(0,1.2e-6,step);
x = linspace(-0.1e-6,0.1e-6,step);
y=linspace(-0.1e-6,0.1e-6,step);
z1 = 1e-6;
L = 9.995498999999999e-07;
a1 = 8.999974099901931e-10;
s = linspace(-L,L,1e5);
F0 = 3.15e7;
Mat_V = zeros(length(z),length(x),length(y));
Temp = Mat_V;
for i = 1 : length(z)
zi = z(i);
for j = 1 : length(x)
xj = x(j);
for k= 1:length(y)
yk = y(k);
if abs(sqrt(xj.^2+yk.^2)) > sqrt((a1*z1)*(1-(zi*zi/z1/z1)))
F_f = s ./((sqrt(xj.^2+yk.^2) + (zi-s).^2).^0.5);
temp = z1 / (z1 * log((L+z1)/(-L+z1)) - 2 * L) * (trapz(s,F_f));
V_f = F0 * (temp - zi);
Mat_V(i,j,k) = V_f;
Temp(i,j,k) = temp;
else
Mat_V(i,j,k) = nan;
end
end
end
end
[X,Y,Z] = meshgrid(x,y,z) ;
F = permute(Mat_V, [2 3 1]); %created as z x y
fig = figure();
[Fmin, Fmax] = bounds(F, 'all');
Fvec = linspace(Fmin, Fmax, 6);
for K = 1 : length(Fvec)
isosurface(X, Y, Z, F, Fvec(K));
end
set(findobj(fig, 'type', 'patch'), 'FaceAlpha', 0.5)
[Tmin, Tmax] = bounds(Temp, 'all')
Walter Roberson
on 25 Jul 2022
format long g
step = 10;
z = linspace(0,1.2e-6,step);
x = linspace(-0.1e-6,0.1e-6,step);
y=linspace(-0.1e-6,0.1e-6,step);
z1 = 1e-6;
L = 9.995498999999999e-07;
a1 = 8.999974099901931e-10;
s = linspace(-L,L,1e5);
F0 = 3.15e7;
Mat_V = zeros(length(z),length(x),length(y));
Temp = Mat_V;
for i = 1 : length(z)
zi = z(i);
for j = 1 : length(x)
xj = x(j);
for k= 1:length(y)
yk = y(k);
if abs(sqrt(xj.^2+yk.^2)) > sqrt((a1*z1)*(1-(zi*zi/z1/z1)))
F_f = s ./((sqrt(xj.^2+yk.^2) + (zi-s).^2).^0.5);
temp = z1 / (z1 * log((L+z1)/(-L+z1)) - 2 * L) * (trapz(s,F_f));
V_f = F0 * (temp - zi);
Mat_V(i,j,k) = V_f;
Temp(i,j,k) = temp;
else
Mat_V(i,j,k) = nan;
end
end
end
end
[X,Y,Z] = meshgrid(x,y,z) ;
F = permute(Mat_V, [2 3 1]); %created as z x y
fig = figure();
[Fmin, Fmax] = bounds(F, 'all');
Fvec = linspace(Fmin, Fmax, 6);
for K = 1 : length(Fvec)
isosurface(X, Y, Z, F, Fvec(K));
end
set(findobj(fig, 'type', 'patch'), 'FaceAlpha', 0.5)
title('colored by MAT_V')
fig2 = figure()
[Tmin, Tmax] = bounds(Temp, 'all');
Tvec = linspace(Tmin, Tmax, 5);
for K = 1 : length(Tvec)
isosurface(X, Y, Z, Temp, Tvec(K));
end
set(findobj(fig2, 'type', 'patch'), 'FaceAlpha', 0.5)
title('colored by MAT_V without -F0*z term')
More Answers (0)
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


