Need help getting this code to plot
显示 更早的评论
I want to plot the capacitance in this graph (cap) vs. the grid size to show how (in theory) it should get moe and more exact as grid size gets smaller. But I can't figure out how to do it. I tried putting it in the for loop, and outside the for loop, but have made no progress. Can someone help me figure out how to do this plot?
% --------------------------------------------------------------% Compute capacitance per unit length of
% a coaxial pair of rectangles
% -------------------------------------------------------------
function cap = capacitor(a, b, c, d, n, tol, rel)
% Arguments:
a = 0.015; %width of inner conductor
b = 0.015; %height of inner conductor
c = 0.02; %width of outer conductor
d = 0.02; %height of outer conductor
n = 10; %number of points in the x-direction (horizontal)
tol = 0.001; %relative tolerance for capacitance
rel = 1.7; %relaxation parameter
% (a good choice is 2-c/n, where c is about pi)
% Returns:
% cap = capacitance per unit length [pF/m]
% Make grids
h = 0.5*c/n; % Grid size
na = round(0.5*a/h); % Number of segments on ’a’
x = linspace(0,0.5*c,n+1); % Grid points along x-axis
m = round(0.5*d/h); % Number of segments on ’d’
mb = round(0.5*b/h); % Number of segments on ’b’
y = linspace(0,0.5*d,m+1); % Grid points along y-axis
% Initialize potential and mask array
f = zeros(n+1,m+1); % 2D-array with solution
mask = ones(n+1,m+1)*rel; % 2D-array with relaxation
% [mask(i,j) = 0 implies
% unchanged f(i,j)]
for i = 1:na+1;
for j = 1:mb+1;
mask(i,j) = 0;
f(i,j) = 1;
end
end
% Gauss Seidel iteration
oldcap = 0;
for iter = 1:1000000; % Maximum number of iterations
f = seidel(f,mask,n,m); % Perform Gauss-Seidel iteration
cap = gauss(n,m,h,f);% Compute the capacitance
if (abs(cap-oldcap)/cap<tol);
break % Stop if change in capacitance
% is sufficiently small
else
oldcap = cap; % Contiue until converged
end
end
str = sprintf('Number of iterations = %4i',iter); disp(str);
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!