Mathematica学习笔记
mathematica将变量导出为matlab代码
Import["C:\\Users\\mengx\\Desktop\\ToMatlab.m", "Package"]
thisDir = ToFileName[
("FileName" /. NotebookInformation[EvaluationNotebook[]])
[[1]]];
SetDirectory[thisDir];
<< ToMatlab.m
ToMatlab[r3]
1. (*exam1*)
sols = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}];
{x, y} /. sols[[1]]
2. (*exam2*)
Array[a, 2]
b = Solve[{a[1] + a[2] == 2, a[1] == 3}, {a[1], a[2]}]
For[k = 1, k < 3, k++, a[k] = a[k] /. b[[1, k]]]
all = {x1, x2}
bb = Solve[{x1 + x2 == 2, x1 == 3}, {x1, x2}]
For[k = 1, k < 3, k++, all[[k]] = all[[k]] /. bb[[1, k]]]
化简
SC1=Simplify[C1]
使用mathematica绘图
Clear["Global`*"]
r1 = Sqrt[I*\[Omega]/\[ScriptCapitalD]];
\[ScriptCapitalA] = \[Alpha]*\[ScriptCapitalD]/(I*\[Omega]*(r1*(E^(-2*
r1*L) - 1) - \[Alpha]*(E^(-2*r1*L) + 1)));
\[ScriptH][x_] =
R*(\[ScriptCapitalA]*(E^(r1*x - 2*r1*L) +
E^(-r1*x)) + \[ScriptCapitalD]/(I*\[Omega]));
\[Alpha] = 0.01; \[Omega] =
2*Pi/10; K = 1; hm = 10; L = 100; R = 0.02; Sy = 0.1;
\[ScriptCapitalD] = K*hm/Sy;
Plot[ComplexExpand[Re[\[ScriptH][x]]], {x, 0, 100},
AxesLabel -> {"x(m))", "h(m)"}, PlotStyle -> Thickness[0.01],
BaseStyle -> {FontWeight -> "Bold", FontSize -> 16}]
(*上述设置了线宽和坐标轴刻度字体大小*)
(*清除全局变量*)
Clear["Global`*"]
(*求解常微分方程*)
sol = DSolve[h''[x] + R == I*\[Omega]/\[ScriptCapitalD]*h[x], h[x], x];
(*变量替换*)
h[x_] = h[x] /. sol[[1]] /. C[1] -> C1 /. C[2] -> C2;
(*求导*)
Dh[x_] = Derivative[1][h][x];
(*求解待解系数C1,C2*)
solC1C2 = Solve[{Dh[0] == \[Alpha]*h[0], Dh[L] == 0}, {C1, C2}];
C1 = Simplify[C1 /. solC1C2[[1]]];
C2 = Simplify[C2 /. solC1C2[[1]]];
\[ScriptH][\[Omega]_, \[Alpha]_] = h[x];
(*参数*)
K = 1; hm = 10; L = 100; R = 0.02; Sy = 0.1; x = 10;
\[ScriptCapitalD] = K*hm/Sy;
(*绘图添加图例,要先导入图例包*)
Needs["PlotLegends`"]
LogLogPlot[{\[ScriptH][\[Omega], 0.001]*
Conjugate[\[ScriptH][\[Omega], 0.001]], \[ScriptH][\[Omega], 0.1]*
Conjugate[\[ScriptH][\[Omega], 0.1]], \[ScriptH][\[Omega], 100]*
Conjugate[\[ScriptH][\[Omega], 100]]}, {\[Omega], 0.0001, 100},
PlotLegend -> {"\[Alpha]=0.001", "\[Alpha]=0.1", "\[Alpha]=100"},
AxesLabel -> {"\[Omega](1/d)",
"\!\(\*SubscriptBox[\(S\), \(hh\)]\)/\!\(\*SubscriptBox[\(S\), \
\(RR\)]\)"}, PlotStyle -> Thickness[0.01]]
(*添加了图例、设置了线宽、设置坐标轴标签*)