Clear Filters
Clear Filters

Solve a function and plot its contour plot. Not getting the desired contour plot?

4 views (last 30 days)
I have defined the temperature field as Z..and want to plot the temperature contour. However, I am unable to get the desired contour plot. Can someone please help me with this? I have also trield fcontour by defining X,Y as variables..but with no results.
P = 50;
v = 0.1;
k = 113;
Tm = 843;
T0 = 300;
a = 4.63 * 10^(-5);
eps = 0.9;
sig = 5.67 * 10^-8;
A = 10^-5;
kp = 0.21;
x = linspace(-3, 3);
y = linspace(-3, 0);
% Remove NaN values by replacing them with a default value (e.g., 0)
x(x == 0) = 0;
y(y == 0) = 0;
[X, Y] = meshgrid(x, y);
% Ensure that r is not zero to avoid division by zero issues
r = sqrt((X.*(10^-3)).^2 + (Y.*(10^-3)).^2);
r(r == 0) = 10^-6; % Replace zeros with a small value (eps) to avoid division by zero
Z = (1./(4*k*pi.*r.*(Tm-T0))) .* (P * exp((-v.*(r+X.*10^-3))./(2*a)) - A*(h.*(Z-T0)+eps*sig*(Z.^4-T0^4)+(kp.*(Z-T0)./r)));
figure
contourf(X, Y, Z)
colorbar;
  8 Comments
Walter Roberson
Walter Roberson on 5 Dec 2023
Because of the Z.^4 on the right hand size, you are defining a quartic -- a polynomial in degree 4. There are 4 solutions for each point. An even number of those solutions will be real-valued.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Dec 2023
%h was not defined in original code -- make sure you assign a meaningful
%value!
h = 1;
syms X Y Z real
Q = @(v) sym(v);
P = Q(50);
v = Q(0.1);
k = Q(113);
Tm = Q(843);
T0 = Q(300);
a = Q(463) * Q(10)^(-7);
eps = Q(0.9);
sig = Q(567) * Q(10)^-10;
A = Q(10)^-5;
kp = Q(0.21);
Pi = Q(pi);
R = sqrt((X.*(Q(10)^-3)).^2 + (Y.*(Q(10)^-3)).^2);
r = piecewise(R == 0, 1e-6, R);
eqn = Z == (1./(4*k*Pi.*r.*(Tm-T0))) .* (P * exp((-v.*(r+X.*10^-3))./(2*a)) - A*(h.*(Z-T0)+eps*sig*(Z.^4-T0^4)+(kp.*(Z-T0)./r)));
zsol = solve(eqn, Z, 'returnconditions', true)
zsol = struct with fields:
Z: [6×1 sym] parameters: [1×0 sym] conditions: [6×1 sym]
x = linspace(-3, 3);
y = linspace(-3, 0);
[xG, yG] = meshgrid(x, y);
%warning: zsolfun returns a matrix and must be invoked on scalars!
zsolfun = matlabFunction(reshape(zsol.Z, 1, 1,[]), 'File', 'zsol.m', 'Vars', [X, Y], 'optimize', false);
zcondfun = matlabFunction(reshape(zsol.conditions, 1, 1, []), 'File', 'zcond.m', 'Vars', [X, Y], 'optimize', false);
[xG, yG] = meshgrid(x, y);
Zcell = arrayfun(zsolfun, xG, yG, 'uniform', 0);
Zmat = cell2mat(Zcell);
Zcondcell = arrayfun(zcondfun, xG, yG, 'uniform', 0);
Zcond = cell2mat(Zcondcell);
for L = 1 : size(Zcond,3)
mask = ~Zcond(:,:,L);
layer = Zmat(:,:,L);
layer(mask) = NaN;
if nnz(~isnan(layer)) == 0; continue; end
figure;
subplot(2,1,1)
contour(xG, yG, layer, 7);
colorbar();
title("root #" + L);
subplot(2,1,2)
scatter(xG(:), yG(:), [], layer(:));
colorbar();
end

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!