CS计算机代考程序代写 matlab [Content_Types].xml

[Content_Types].xml

_rels/.rels

matlab/document.xml

matlab/output.xml

metadata/coreProperties.xml

metadata/mwcoreProperties.xml

metadata/mwcorePropertiesExtension.xml

metadata/mwcorePropertiesReleaseInfo.xml

MATLAB Boundary Value Problems In this livescript, you will learn how To solve boundary value problems in MATLAB Many physical problems are governed by systems of differential equations. For example, the long-term heat distribution over a one-dimensional piece of metal is given by the heat equation \frac{\partial T}{\partial t}=\frac{\partial^{2}T}{\partial x^{2}} along with prescribed temperatures at the ends. Essentially, the way these problems work is that we can prescribe boundary data, by say heating the ends, and the differential equation tells us how the temperature changes within the rod. For simplicity we’ll consider the solution of the steady-state heat equation in one dimension \frac{d^{2}T}{dx^{2}}=0 with the boundary conditions T(0)=0 and T(1)=1 , since we can very easily derive its analytical solution. (a) Show that the temperature distribution is given by T(x)=x In case the differential equation is hard to solve, the symbolic package in MATLAB is capable of solving some boundary value problems using \texttt{dsolve} . syms T(x) x
dsolve(diff(T,2)==0,T(0)==0,T(1)==1) I would recommend throwing the BVP into \texttt{dsolve} first. It’s usually worth a shot to see whether an analytical solution is possible. Most of the time \texttt{dsolve} won’t like the BVP, which means that we’ll have to solve it numerically. Unfortunately MATLAB’s numerical solver is quite picky with the how the differential equation is specified. We first need to write our differential equation as a system of first order equations. Let T_{1}(x)=T(x)\\
T_{2}(x)=T'(x) Then we can see that T_{2}(x)=T_{1}'(x)=T'(x) which gives us our first equation. Now, using the heat equation, we have T’_{2}(x)=0 So our system of differential equations is T’_{1}(x)=T_{2}(x)\\
T’_{2}(x)=0 As for the boundary conditions, they become T_{1}(0)=0\\
T_{1}(1)=1 To solve this, MATLAB has the functions \texttt{bvp4c} and \texttt{bvp5c} which use fourth and fifth order methods respectively. The syntax of these functions is the same, so we’ll just cover the use of \texttt{bvp4c} \texttt{sol = bvp4c(odefun,bcfun,solint)} Here, \texttt{odefun} is a function of the differential equations to solve, which is specified as a function handle. MATLAB only accepts systems of first order differential equations in \texttt{odefun} , so writing the two equations we found before in vector form, we have \pmatrix{
T_{1}'(x) \cr
T_{2}'(x)
}
=
\pmatrix{
T_{2}(x) \cr
0
} Therefore a MATLAB function that computes the derivatives is function dTdx = odefun(x,T_sys) dTdx = [T_sys(2);0] end \texttt{bcfun} is a function that defines the boundary conditions. It’s written as a function handle that computes the residual error at the boundary. What this means is that it determines the difference between the computed boundary value and the actual value. If T^{(n)}(b) is the n-th iterate and T_{b} is the boundary condition at x=b , the residual R is given by R=T^{(n)}(b)-T_{b} which should approach zero as more iterates are computed and it approaches the true solution. For the heat equation, our boundary conditions are T_{1}(0)=0\\
T_{1}(1)=1 which in terms of the residual is \pmatrix{
R_{1} \cr
R_{2}
}
=
\pmatrix{
T_{1}(0)-0 \cr
T_{1}(1)-1
} Therefore, a MATLAB function that computes the residual is function res = bcfun(Ta,Tb) res = [Ta(1);Tb(1)-1] end Note that \texttt{Ta} and \texttt{Tb} have the form \pmatrix{
\texttt{Ta(1)} \cr
\texttt{Ta(2)}
}
=
\pmatrix{
T_{1}(a) \cr
T_{2}(a)
} so the index of 1 in the function above is just to specify the temperature boundary condition at $x=a$ . If our boundary condition was specified in terms of the derivatives, the index would be 2. Finally, \texttt{solint} is an initial guess for the boundary value problem. The way \texttt{bvp4c} works is by solving a nonlinear set of algebraic equations using something like the Newton-Raphson method, which means that we need an initial guess. To do this, we firstly generate a mesh using \texttt{linspace} . For our purposes, we have an evenly spaced grid between 0 and 1 with 5 nodes. xmesh = linspace(0,1,5); Then, we can use \texttt{bvpinit} to generate our guess. This takes two inputs, the first being our mesh \texttt{xmesh} and the second is a function that generates our initial guess at each grid point. Because our differential equation is quite simple in structure, most guesses will converge, so we can just set it as 1 for all x . function g = guess(x) g = [1;1]; end Therefore, our initial guess is solint = bvpinit(xmesh,@guess) But in general, for nonlinear differential equations, choosing initial guesses is more of an art. The wrong choice will most likely cause the solution to diverge, which you would have already seen when using open methods with values far away from the root. Once we’ve got all the inputs, we may call \texttt{bvp4c} using the following command. sol = bvp4c(@odefun,@bcfun,solint) And plotting gives plot(sol.x,sol.y,’.-‘,’MarkerSize’,15)
legend(‘T_{1}(x)’,’T_{2}(x)’)
xlabel(‘x’)
ylabel(‘Temperature (T)’) Code Fragments function dTdx = odefun(x,T)
dTdx = [T(2); 0];
end

function res = bcfun(Ta,Tb)
res = [Ta(1); Tb(1)-1];
end

function g = guess(x)
g = [1; 1];
end

manual code ready 0.4 true false 0 17 17 false true 1 18 18 true false 2 60 60 false true 3 65 65 true false 4 69 69 false false 5 71 71 false false 6 72 72 false false 7 73 73 false true 8 74 74 true false 9 77 77 false false 10 78 78 false true 11 79 87

2020-04-30T11:26:10Z 2020-09-27T02:53:30Z

application/vnd.mathworks.matlab.code MATLAB Code R2020b

9.9.0.1444674 2dfee1f6-2b41-4fb1-bee2-75bfc1626a85

9.9.0.1467703
R2020b

Aug 26 2020
2314982500