Is it possible to vary 5 variables in an equation using linspace and plot 3 of them using contour?

1 view (last 30 days)
I have an equation of a physical system and I want to vary 5 parameters (i, N, r1, r2 and L) using linspace or :. My aim to find that an optimum configuration (i.e. a set of values of these variables) that gives me the maximum value of field. However, I get an empty plot when I run this code and I also get an error saying "Undefined function or variable 'field'."
Moreover, I get the values of t =r2-r1 as zero for all values in the workspace.
I would really appreacite if someone could kindly help me.
Thank you in advance.
uo = 1.26*(10^-3);
L=linspace(1,70);
N=linspace(100,1000,10);
i=linspace(1,5,10);
r1=linspace(1,50,100);
r2=linspace(1,50,100);
x1= 10;
x2= x1+L;
t=r2-r1;
[L,t] = meshgrid(L,t);
if r2>r1
g=sqrt((r2.^2)+(x2.^2));
m=sqrt((r1.^2)+(x2.^2));
n=sqrt((r2.^2)+(x1.^2));
o=sqrt((r1.^2)+(x1.^2));
A = reallog((g+r2)./(m+r1));
B = reallog((n+r2)./(o+r1));
field = ((uo.*N.*i)./(2.*L*(r2-r1))).*((x2.*A)-(x1.*B));
end
figure
contour(L,t,field)
  1 Comment
Walter Roberson
Walter Roberson on 2 Feb 2020
field is not defined unless r2>r1 . However,
r1=linspace(1,50,100);
r2=linspace(1,50,100);
so they are both vectors of the same size and same content, so r2 > r1 is never true because r2 == r1

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 2 Feb 2020
Edited: Walter Roberson on 2 Feb 2020
uo = 1.26*(10^-3);
Lvec = linspace(1,70);
Nvec = linspace(100,1000,10);
ivec = linspace(1,5,10);
r1vec = linspace(1,50,100);
r2vec = linspace(1,50,100);
[L, N, i, r1, r2] = ndgrid(Lvec, NVec, ivec, r1vec, r2vec);
x1= 10;
x2= x1+L;
t=r2-r1;
Z = zeros(size(r2));
field = Z;
mask = r2 > r1;
g = sqrt((r2(mask).^2)+(x2(mask).^2));
m = sqrt((r1(mask).^2)+(x2(mask).^2));
n = sqrt((r2(mask).^2)+(x1(mask).^2));
o = sqrt((r1(mask).^2)+(x1(mask).^2));
A = reallog((g+r2(mask))./(m+r1(mask)));
B = reallog((n+r2(mask)./(o+r1(mask))));
field = ((uo.*N(mask).*i(mask))./(2.*L(mask)*(r2(mask)-r1(mask)))).*((x2(mask).*A)-(x1(mask).*B));
[maxfield, maxixd] = max(field);
LNir1r2 = [L(maxid), N(maxid), i(maxid), r1(maxid), r1(maxid)]; %parameter values at maximum
However, do not try to contour field: it is a 5 dimensional variable. You can do things like a "maximum projection" over a 2D subset of the values:
p134 = squeeze(max(field, [], [1 3 4]));
contour(Nvec, r2vec.', p134);

Categories

Find more on Matrices and Arrays 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!