论文中的 Matlab 代码

论文中的 Matlab 代码

我想通过复制和粘贴我想要的代码来插入 matlab 代码,我希望它是黑色的,没有颜色,如果可能的话,我希望在文本框中输入行数,但如果太难的话就没有必要了。以下是我想出的办法:

\documentclass[a4paper,12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,italian]{babel}
\usepackage{url,amsfonts,epsfig}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[backend=biber]{biblatex}
\addbibresource{capitoli/bibliografia/bibliografia.bib}
\usepackage{matlab-prettifier} 

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0.5pt}
\fancyhead[]{} 
\fancyhead[LO]{\nouppercase{\rightmark}} 
\fancyhead[RE]{\nouppercase{\leftmark}} 
\fancyfoot{}
\fancyfoot[LO,RE]{\thepage} 
\renewcommand{\sectionmark}[1]{\markright{#1}}
\parindent 0ex %serve per l'indentadure dei paragrafi


\usepackage{tikz}
\usepgflibrary{arrows.meta}
\usetikzlibrary{calc,quotes,angles} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} 
\usepgfplotslibrary{groupplots} 
\usetikzlibrary{calc} 



\usepackage{hyperref} 


\begin{document}

\section{Greitzer per steady-flow}

`\begin{lstlisting}
clear
clc
close all

%% Gas properties
a01 = 340;    %speed of sound in gas(m/s)
p01 = 1e5;    %Compressor inlet pressure(Pa)
P.T01 = 303.35; %Compressor inlet temperature(K)
P.cp = 1005;    %specific heat capacity for constant pressure, property of gas(J/(kg*K))
P.ro1 = 1.15;   %Gas density(kg/m^3)
Re = 100000;  %Reynolds number()
P.k = 1.4;      %Gas constant ratio, cp/cv()

%% Compressor characteristics
Lc = 0.53;                                          %Length of compressor and duct(m)
Vp = 0.01;                                          %Plenum volume(m^3)
Ac = 0.0026;
% kl = 0.0008;                                        %Throttle gain, proportional to the throttle opening
Dt1 = 0.074;                                        %Impeller diameter at inducer tip(m)
Dh1 = 0.032;                                        %Impeller diameter at hub casing(m)
P.D1 = 1/sqrt(2)*sqrt((Dt1)^2+(Dh1)^2);               %Average diameter(m)
Di = 0.02;                                          %Mean hydraulic diameter of impeller(m)
Dd = 0.02;                                          %Mean hydraulic diameter of impeller(m)
I = 0.001;                                          %Moment of inertia for the compressor spool(kg*m^2)
alfa1 = pi/2;                                       %Flow angle at inducer, for alpha1 = pi/2 rad, assumed no pre-whirl
A = ((pi*P.D1^2)/4)*4;
P.Ai = ((pi*P.D1^2)/4);                                 % Cross section area impeller(m^2)
P.Ad = ((pi*P.D1^2)/4);                                 % Cross section area diffuser(m^2)
li = 0.053;                                         %Mean channel length(m)
ld = 0.053;                                         %Mean channel length(m)
P.beta1b = 0.61;                                      %Impeller blade inlet angle()
Ch = 4*0.3164*(Re)^-0.25;                           %Friction loss coefficient()
P.kf = (Ch*li)/(2*Di*P.ro1^2*P.Ai^2*(sin(P.beta1b))^2)*4;   %Friction loss impeller
P.kfd = (Ch*ld)/(2*Dd*P.ro1^2*P.Ad^2*(sin(P.beta1b))^2)*4;  %Friction loss diffuser
P.sigma = 0.9;                                        %Slip factor()
P.D2 = 0.128;                                         %Diameter of impeller tip(m)
P.alfa2b = atan((P.D1*tan(P.beta1b))/(P.sigma*P.D2));
m = [-0.2:0.001:0.8];                                 %[kg/s] Mass Flow
cn=10;

%% Speed
giri = 55000;   
P.U1 = (P.D1*pi.*giri)/60;
P.U2 = (P.D2*pi.*giri)/60;

%% Greitzer's parameters
wh = a01*(Ac/Vp*Lc)^0.5; %Helmotz's frequency
P.B = P.U1/(2*wh*Lc);        %Greitzer's parameter

%% Efficency Losses 
deltanbf = 0.02;                     %Back flow loss() 0.02 annular diffuser - 0,03 medium volume - 0.05 vaned diffuser
deltanv = 0.035;                     %Volute loss()
deltanc = 0.00;                      %Clearance loss()
deltand = 0.00;                      %Diffusion loss()
P.deltan = deltanbf+deltanv+deltanc+deltand;

%% Energy Losses
deltahfi = P.kf.*(m.^2);    %Impeller Friction losses
deltahfd = P.kfd.*(m.^2);   %Diffuser Friction losses
%% Throttle valve's parameters

%% con lo stesso Delta da una parte e dall'altra
deltaideal = P.sigma*(P.U2^2); %Deltah0c,ideal
  
    %% Lossees    
    deltahii = 0.5.*((P.U1-...
        ((cot(P.beta1b).*m)/(P.ro1*P.Ai))).^2); %Impeller Incidence Loss
    deltahid = 0.5.*(((P.sigma*P.D2*P.U1)/P.D1)-...
        ((m.*cot(P.alfa2b))/(P.ro1*P.Ad))).^2; %Diffuser Incidence Loss
    deltaloss = deltahii+...
        deltahid+deltahfi+deltahfd;
    deltah0c = deltaideal-...
        deltaloss-P.deltan; %deltah0c 
    
    %% Efficiency
    eta = deltaideal./(deltaideal+deltaloss)-P.deltan;

    %% Pressure Rise in the Compressor
    psi_c =((1+((eta.*deltaideal)/(P.T01*P.cp))).^(P.k/(P.k-1)));


%% Throttle valve's parameters

gamma_T = .17;                          %throttle valve aperture 
Delta_gamma_T = .06;                     %range between your gamma_T_max e gamma_T_min
gamma_T_max = gamma_T+(Delta_gamma_T/2);  %maximum aperture
gamma_T_min = gamma_T-(Delta_gamma_T/2);  %minimum aperture

P.Amplitude = (gamma_T_max - gamma_T_min)/2;        %amplitude
P.b = (gamma_T_max + gamma_T_min)/2;

%% Compressor's frequency (only frequency)

compressor_frequency = 10;     %engine's frequency [Hz] 
P.w = compressor_frequency*2*pi; %engine's frequency [radiants/s] 

% save main_parameters.mat

%% Ode's parameters

t0 = 0;  %simulation's start [s]
tf = 200; %simulation's finish [s]
y1 = 0;  %initial condition 1
y2 = 0;  %initial condition 2
init = [y1 y2]'; %initial conditions vector

sample_frequency = 400; %sample frequency [Hz] (choose at least a frequency  
                         % 10-12 times the one being analyzed
                         % for a good definition. Just 2-3 times for a quick try)

maximum_time_step = 1/sample_frequency;
options= odeset('MaxStep',maximum_time_step); %maximum time-step size

[t,y]=ode113(@(t,y)greitzer(t,y,P),[t0,tf],init,options); %I found ode113 is way more efficient

psi_c_y = ((1+((eta.*deltaideal)/(P.T01*P.cp))).^(P.k/(P.k-1)));
valve = (1/gamma_T.^2).*y(:,1).^2;

%% Plots

subplot(2,2,1) %compressor map quadrant
plot(m,psi_c,y(:,1),y(:,2),y(:,1),valve) 
legend('compressor characteristic','\Phi vs \Psi')
grid on
grid minor
xlabel('\Phi')
ylabel('\Psi')
 axis([0 0.8 0 3]);
 
subplot(2,2,2) %compressor map quadrant
plot(m,psi_c_y,y(:,1),y(:,2)) 
hold on
plot(y(:,1),valve)
legend('compressor characteristic','\Phi vs \Psi')
grid on
grid minor
xlabel('\Phi')
ylabel('\Psi')
axis([0 0.8 0 3]);

subplot(2,2,3) % mass flow  quadrant
plot(t,y(:,1))
grid on
grid minor
xlabel('t [s]')
ylabel('\Phi')

subplot(2,2,4) %pressure oscillations quadrant
plot(t,y(:,2))
grid on
grid minor
xlabel('t [s]')
ylabel('\Psi')
\end{lstlisting}
`
\end{document}

通过此代码我得到如图所示的结果: 在此处输入图片描述

答案1

  • 您正在使用该包matlab-prettifier
  • 此包内有说明书,请参阅这里
  • 有一个Matlab-bw选项,请参见下面的屏幕截图,它可以解决您在评论中提到的问题"I partially solved the problem by putting: \begin{lstlisting}[style=Matlab-editor] and it formats as I wanted, but i still get colors, can I have it in black?"
  • 您的代码示例远非简单,下次请花更多精力提供一个“好”问题,请参阅这里这里

在此处输入图片描述

相关内容