Transforming a matlab code

In summary, the code GKLmagnet2.m simulates the magnetic field of two concentric solenoidal superconducting magnets for use in the 140GHz Gyroklystron experiment at MIT. It allows for the specification of the maximum magnetic field and percent homogeneity in the uniform region. The code also includes visualizations of the magnetic field profile and uniformity zones.
  • #1
Radinax
12
0
Hi fellows!
I need a bit of help with transforming a MATLAB code that simulate magnetic field of two concentric solenoid TO a plot that simulate the magnetic fields of high voltage transmission lines (specificly two and four lines), how can it be modified? here is the code:

% GKLmagnet2.m: magnetic profile for TWO ring magnets.
% This code simulates the magnetic field on the z-axis of
% two concentric solenoidal superconducting magnets of specified
% size for use in the 140GHz Gyroklystron (GKL) experiment, MIT.
% With two coils, it's possible to have one large region that
% meets the tolerance spec, otherwise two fields are created which
% may not meet spec. If the field is not continuous (one field)
% to within spec, the varialbe 'flag' is set to unity (otherwise 0),
% and a warning is displayed.
%
% run GKLmagnet2
%
% Last edit by Colin Joye, 7/10/03
clear all;
opengl autoselect;
% -------------- USER INPUT -------------------------
Bmax = 5.12; %[Telsa] maximum magnetic field in uniform region
Tol = 0.3; %[%] percent homogeneity of uniform region

% Magnet size. The magnet is a finite-length finite-thickness hollow cylinder
% modeled as a single turn rectangular cross-section current density.
% The magnet is centered at z=0, extending 'Length'/2 above and below z=0.
RadIn = 15; %[cm] inner radius of coil
RadOut = 18; %[cm] outer radius of coil
Length = 6; %[cm] coil length
Separ = 18.0; %[cm] center separation distance > Length
zaxis = 30; %[cm] length above z=0 to view the field profile

% -------------- End USER INPUT ---------------------

% extra user constants:
transparency = 0.25; % controls transparency of slices & patches. '= 1.0' is opaque.
cyl_frac = 0.74; % azimuthal fraction of cylinder to plot. '= 1.0' for full cyl.

% check if length is larger than separation.
if(Separ<Length),
disp(['Error, Separation must be greater than Length']);
break;
end
flag = 0;

% Define z-axis vector for field profile.
z = zaxis*[-1:1/2e4:1];
%z = [0 1 2 5 10 15 20 25 30 31]; %Kreischer's Table II.

% Draw cylinders
sep = Separ/2;
L = Length;
[Xi,Yi,Zi] = cylinder(RadIn,100);
[Xo,Yo,Zo] = cylinder(RadOut,100);
s = floor( cyl_frac*size(Xi,2) )+1;
cylXi = (Zi(:,1:s)-0.5)*L;
cylYi = -Xi(:,1:s);
cylZi = -Yi(:,1:s);
cylXo = (Zo(:,1:s)-0.5)*L;
cylYo = -Xo(:,1:s);
cylZo = -Yo(:,1:s);

% Applying Biot-Savart law to a finite-length finite-thickness cylinder
% with a rectangular cross-section of uniform current density, we get,
% using an analytic (exact) result from Maple v8.0:
r1 = RadIn;
r2 = RadOut;
Bz = zeros(1,length(z));
for M=1:2, % loop calculates for both magnets.
r1p = r1 + sqrt(r1^2 + (L/2 + (z + sep)).^2);
r1n = r1 + sqrt(r1^2 + (L/2 - (z + sep)).^2);
r2p = r2 + sqrt(r2^2 + (L/2 + (z + sep)).^2);
r2n = r2 + sqrt(r2^2 + (L/2 - (z + sep)).^2);
C0 = (z + sep) .* log( r1n./r1p .* r2p./r2n );
C1 = L/2 * log( r2n .* r2p ./ r1n ./ r1p );
A0 = 1 / (r2 - r1) / L; % multiplicative value
Hz = A0 * (C0 + C1);
Bz = Bz + Hz * Bmax / max(Hz); % normalizing for desired value of Bmax.
sep = -sep; % reverse sign for other magnet, reverse again on exit.
end
% Renormalize again, since addition of field messes up first normalization.
Bz = Bz*Bmax/max(Bz);

% Find uniformity zones.
zmid = find(z==0);
u = find( abs(Bz(zmid:end) - Bmax)/Bmax <= (Tol/100) );
UniformLength = 2*(z(u(end)+zmid)-z(u(1)+zmid));
if(u(1)~=1), %check if the field is still continuous.
disp(['Warning, field is broken into two regions']);
flag = 1;
end

% ---------------- Figures -------------------
figure(1)
clf(1)
hold on;
surf(cylXi-sep,cylYi,cylZi); % inner cylinder
surf(cylXo-sep,cylYo,cylZo); % outer cylinder
surf(cylXi+sep,cylYi,cylZi); % inner cylinder
surf(cylXo+sep,cylYo,cylZo); % outer cylinder
colormap autumn;
shading flat;

% Plot dressups
a = 1.2*RadOut;
b = 1.1*zaxis;
axis equal;
axis([-b b -a a -a a]);
view([-15 15]);

% define scaling variable for making field plot look better.
scale = floor(a/max(Bz)); % scale line and grid to view field better
if(scale<=1) % make sure you don't have scale = 0, 3, 6, 7, 9, etc.
scale=1;
elseif(mod(scale,2)~=0 & mod(scale,5)~=0)
scale=scale-1;
end

% Add transparent slices. Transparency controlled by "alpha"
% Horizontal plane slice
h = patch([-b -b b b],[-a a a -a],[0 0 0 0],[0.2 1 0.5]);
alpha(h,transparency);
set(h,'EdgeAlpha',0);
% Vertical plane slice
h = patch([-b -b b b],[0 0 0 0],[-a a a -a],[0.2 1 0.5]);
alpha(h,transparency);
set(h,'EdgeAlpha',0);
% Z=0 plane slice
%h=patch([0 0 0 0],[-a -a a a],[-a a a -a],[0.7 1 0]);
%alpha(h,.3);
%set(h,'EdgeAlpha',0);

% solidify cylinders
map = colormap;
px = cylXi(:,end)'*[1 0 0 1;0 1 1 0];
py = [cylYi(:,end)' cylYo(:,end)'];
pz = [cylZi(:,end)' cylZo(:,end)'];
h = patch(px-sep, py, pz,[0.7 0.8 0.9]);
set(h,'EdgeAlpha',transparency);
h = patch(px+sep, py, pz,[0.7 0.8 0.9]);
set(h,'EdgeAlpha',transparency);
px = cylXi(:,1)'*[1 0 0 1;0 1 1 0];
py = [cylYi(:,1)' cylYo(:,1)'];
pz = [cylZi(:,1)' cylZo(:,1)'];
h = patch(px-sep, py, pz,[0.7 0.8 0.9]);
set(h,'EdgeAlpha',transparency);
h = patch(px+sep, py, pz,[0.7 0.8 0.9]);
set(h,'EdgeAlpha',transparency);

% Put grid on field line patch
ytick = get(gca,'Ztick');
ytick = (ytick + max(ytick))/2; % remove negative ticks and interpolate.
ztick = get(gca,'Xtick');
Ly = length(ytick);
Lz = length(ztick);
y0 = zeros(1,Ly);
y1 = b*ones(1,Ly);
z0 = zeros(1,Lz);
z1 = a*ones(1,Lz);
line([1;1]*ztick,[z0;z0],[z0;z1],'Color',0.4*[1 1 1],'LineStyle',:); %vert lines
line([-y1;y1],[y0;y0],[1;1]*ytick,'Color',0.4*[1 1 1],'LineStyle',:);%horiz lines
% Add axis line and numbers on new grid.
% Vertical:
line(-[b b],[0 0],[0 a],'Color',[0 0.3 1]); % vert axis line for numbers
line(-[1;1.03]*y1,[y0;y0],[1;1]*ytick,'Color',[0 0.3 1]); % draw tick lines.
h = text(-1.03*y1',y0',ytick',num2str(ytick'/scale),'HorizontalAlignment','right');
set(h,'Color',[0 0.3 1]);
pz = length(get(h,'String'))-3;
h = text(-1.1*b-pz,0,mean(ytick),'Field [T] (blue)','HorizontalAlignment','center');
set(h,'Color',[0 0.3 1],'Rotation',90);
% Horizontal:
line(-[b zaxis],[0 0],[0 0],'Color',[0 0.3 1]); % horiz axis line for numbers
line( [zaxis b],[0 0],[0 0],'Color',[0 0.3 1]); % horiz axis line for numbers
line([1;1]*ztick,[z0;z0],-[0;0.03]*z1,'Color',[0 0.3 1]); % draw tick lines.
h = text(ztick',z0',-0.055*z1',num2str(ztick'),'HorizontalAlignment','center');
set(h,'Color',[0 0.3 1]);
h = line([-zaxis zaxis],[0,0],[0,0]); % z-axis line.
set(h,'Color','red','LineWidth',2.5);

% Add plot of field line
h = plot3(z,zeros(1,length(Bz)),scale*Bz,'b');
set(h,'Color','blue','Linewidth',2);
h = text(0,0,0.1*Bmax,'Uniform Region');
set(h,'Rotation',90,'Color',[0 0.4 0.4])

% Add patch showing uniform region within 'Tol' percent.
uz0 = u(1)+zmid;
uz1 = u(end)+zmid;
px = [z(uz0) z(uz1) z(uz1) z(uz0)];
py = [0 0 0 0];
pz = scale*[0 0 Bz(u(1)+zmid) Bz(u(end)+zmid)];
h = patch(px, py, pz,[1 0.2 0.5]);
alpha(h,1.5*transparency);
set(h,'EdgeAlpha',0);
px = -[z(uz0) z(uz1) z(uz1) z(uz0)];
h = patch(px, py, pz,[1 0.2 0.5]);
alpha(h,1.5*transparency);
set(h,'EdgeAlpha',0);
%h = plot3(z(u),zeros(1,length(u)),max(Bz)*ones(1,length(u)),'m');
%set(h,'LineWidth',2');
h = text(0,0,0.1*Bmax,'Uniform Region');
set(h,'Rotation',90,'Color',[0 0.4 0.3])

% Title and Labels
title(['Magnet strucure w/ Field: ' num2str(Bmax) ' T max; R1 = ' num2str(r1) ...
' cm; R2 = ' num2str(r2) ' cm; L = ' num2str(L) ' cm; S = ' num2str(Separ) ...
' cm; ' num2str(Tol) '% Uniform length: ' num2str(UniformLength) ' cm']);
xlabel('Z-axis [cm]');
ylabel('X-axis [cm]');
zlabel('Y-axis [cm]');

hold off;

% ------------- END GKLmagnet2.m ---------------------------

PS: It dooes what i need but i need the magnetic fields of those high voltage transsmision lines, and i don't know to much of programing, sooo i will apreciate a loooot if you can help me out =)
 
Physics news on Phys.org
  • #2
I could use some help you know...
 
  • #3
Radinax said:
I could use some help you know...

Ok then. Hang around the Computer Lab while other students from your class are working on this project. After they leave, look though all the waste paper bins in the hope they've disgarded some hard copies of partially debugged code. Then you'll have some code that's a little bit closer to what you're trying to achieve to try and hack.
 
Last edited:
  • #4
Now for a serious reply.

You haven't got any help because you've posted a silly question. No one here is likely to be writting code by "transforming" code from a completely different problem, it's just not how it's done.

You should be starting here with the relevant equations for the actual problem at hand. If you don't know what these are then that's what your first question should be in relation to.
 
  • #5


Hello there,

Thank you for reaching out for help with your MATLAB code. To transform your code to simulate the magnetic fields of high voltage transmission lines, there are a few changes that need to be made. Here are some suggestions:

1. Change the geometry: The first step would be to change the geometry of the coils from concentric solenoids to the geometry of high voltage transmission lines. This would involve changing the equations used to calculate the magnetic field components.

2. Modify the input parameters: The input parameters such as Bmax, Tol, RadIn, RadOut, Length, Separ, and zaxis would need to be modified to fit the geometry of the transmission lines. You may need to do some research to find the appropriate values for these parameters for your specific transmission lines.

3. Change the calculation method: Instead of using the Biot-Savart law, you may need to use a different method to calculate the magnetic field for the transmission lines. This could involve using different equations or integrating the magnetic field over the length of the transmission lines.

4. Add multiple lines: To simulate the magnetic fields of two and four lines, you will need to add multiple lines in your code. This could involve using a loop or defining separate variables for each line.

5. Plot the results: Once you have made the necessary changes to the code, you can plot the magnetic field profiles for the transmission lines using the plot function. You can also add labels and titles to the plot to make it more informative.

I hope these suggestions help you modify your code to simulate the magnetic fields of high voltage transmission lines. Good luck!
 

FAQ: Transforming a matlab code

How can I optimize my MATLAB code for faster execution?

There are a few ways to optimize your MATLAB code for faster execution. One way is to vectorize your code, which means using MATLAB's built-in functions instead of loops. This can significantly improve performance. Another way is to preallocate arrays and minimize the use of global variables. Additionally, using efficient data structures and avoiding unnecessary calculations can also help improve execution speed.

How can I debug my MATLAB code?

To debug your MATLAB code, you can use the built-in debugging tools such as the "dbstop" command to set breakpoints in your code and step through it line by line to identify any errors. You can also use the "dbstack" command to view the current stack of functions and the "dbclear" command to remove breakpoints.

How can I improve the readability of my MATLAB code?

To improve the readability of your MATLAB code, you can use meaningful variable names, indent your code properly, and add comments to explain complex or important sections. You can also break long lines of code into multiple lines and use white spaces to make your code easier to understand.

How can I make my MATLAB code more efficient?

To make your MATLAB code more efficient, you can use the profiler tool to identify any bottlenecks in your code and optimize them. You can also use parallel computing to speed up the execution of certain tasks. Additionally, avoiding unnecessary function calls and using built-in functions instead of writing your own can also improve efficiency.

How can I test the accuracy of my MATLAB code?

To test the accuracy of your MATLAB code, you can use test cases with known inputs and outputs to verify that your code is producing the correct results. You can also use the built-in "assert" function to check if certain conditions are met during the execution of your code. Additionally, comparing your results to those from other sources or using analytical solutions can also help verify the accuracy of your code.

Similar threads

Replies
2
Views
5K
Replies
2
Views
2K
Replies
1
Views
4K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
14
Views
6K
Replies
4
Views
5K
Replies
7
Views
10K
Back
Top