Assignment 3

8 minute read

매트랩에서의 심볼릭 계산

Assignment 3
Score 0
Submission False

기본적인 함수들 외에도, 매트랩에는 수치 계산에 사용될 수 있는 아주 많은 추가적인 툴박스들이 존재합니다. 특정한 매트랩 툴박스를 사용하기 위해서는 각각의 툴박스를 따로 설치해야 합니다. 운이 좋게도, KAIST에 제공되는 매트랩 라이센스에는 15개의 유용한 툴박스가 제공됩니다. 그 중에서 우리는 Symbolic Math Toolbox를 이용하여 심볼릭 계산에 이용하는 방법을 알아보려 합니다.

1. 변수 선언, 인수분해, 근

이 문제에서 우리는 다음 다항함수의 근을 구하려고 합니다:

\[f(x) = x^6 - 11x^5 + 7x^4 + 163x^3 - 164x^2 - 476x + 480.\]
  1. 매트랩 명령어 syms를 이용하여, 심볼릭 변수 \(x\) 를 선언하세요.
  2. 매트랩 명령어 factor를 이용하여, 주어진 다항함수를 인수분해하세요.
  3. 매트랩 명령어 solve를 이용하여, 주어진 다항함수의 근을 구하세요.
  4. 매트랩 명령어 subs를 이용하여, 3번 문항에서 구한 근이 \(f(x)=0\)을 만족한다는 걸 검증하세요.

Solution.

syms x;     % Declare x as symbolic.
y = x^6 - 11*x^5 + 7*x^4 + 163*x^3 - 164*x^2 - 476*x + 480;
% Factorize y with the command factor.
disp('The factorization of f(x) is'); disp(factor(y)); 
% Find the roots of f(x)=0 by using the command solve.
roots = solve(y);
disp('The roots of f(x)=0 are'); disp(roots);
% Substitute each output of solve(y) into x,
% and calculate each function value.
for i = 1:6
    check(i) = subs(y, x, roots(i));
    fprintf('f(%d)= %g\t', i, check(i));
end
fprintf('\n');

2. 미분, 대입, 극한

이 문제에서 우리는 여러가지 미분적분학 문제를 해결하려 합니다.

  1. 매트랩 명령어 diff를 이용하여, 다음에 주어진 함수 \(f\)의 이계도함수 \(f_{xx}\)를 계산하세요:

    \[f(x, y) = \sin(x^2y) + \cos(xy^2).\]

    Solution.

     syms x y;               % Set x, y to be symbols.
     f = sin(x^2 * y);
     fxx = diff(f, x, 2);    % Differentiate f with respect to x twice.
     disp('The 2nd derivative of f with respect to x is'); disp(fxx);
    
  2. (1)에서의 결과와 매트랩 명령어 subs를 이용하여, \(f_{xx}(1, 2)\)를 구하세요. 심볼릭형으로 주어진 결과값을 수치적인 double형으로 변환하기 위해서 double명령어를 이용하세요.

    Solution.

     syms x y;               % Set x, y to be symbols.
     f = sin(x^2 * y);
     fxx = diff(f,x,2);      % Differentiate f with respect to x twice.
     sfxx = simplify(fxx);   % Simplify the expression fxx.
     % Substitute x=1, y=2 into sfxx.
     value = subs(sfxx, [x,y], [1,2]); format rat;
     res = double(value);    % Get a number from a symbol.
     disp('fxx(1,2)='); disp(res);
    
  3. 매트랩 명령어 limit를 이용해서 다음 극한값을 구하세요.

    \[\lim_{h \rightarrow 0} \frac{\cos(x + h) - \cos{x}}{h}\]

    Solution.

     syms x h;           % Set x,h as symbols.
     % Find the limit of the function as h->0.
     res = limit((cos(x+h)-cos(x))/h, h, 0);
     disp('The result of the limit is'); 
     pretty(res);        % Produce a typeset type display.
    

3. 테일러 급수와 시각화

이제 우리는 다음 함수의 테일러 급수를 구해보려고 합니다:

\[f(x) = \frac{1}{5 + \cos{x}}.\]
  1. 매트랩 명령어 taylor를 이용해서 \(x=2\)를 중심으로 전개되는 \(f(x)\)의 테일러 급수를 \(10\)차 항까지 구해보세요. [상수항은 \(0\)차 항이라는 걸 기억하세요.]

  2. 매트랩 명령어인 fplotplot(또한 hold on)를 이용하여 (1)번에서 얻은 다항식을 \(1 \le x \le 3\)범위 안에 그리세요. 그리고, 원래의 함수 \(f(x)\) 역시 같은 figure에 그려보세요.

Solution.

syms x;     % Regard x as a symbol. 
f = 1/(5 + (cos(x)));

% (i). Find the Taylor series expansion of f at x=2 up to the 10th order terms.
T = taylor(f, x, 2, 'order', 11); 

% (ii). Draw the resulting polynomial (i) and the original function f(x).
fplot(T, [1, 3]); hold on;
xd = 1:0.05:3;      yd = subs(f, x, xd);
plot(xd, yd, 'or');
title('Taylor approximation vs actual function');
xlabel('x'); ylabel('y');
legend('Taylor approximation', 'Original function', 'Location', 'best');

4. 극점, 변곡점, 점근선

이 문제에서는 극소값과 극대값, 변곡점 등을 구해보겠습니다.

\[f(x) = \frac{3x^2 + 6x - 1}{x^2 + x - 3}.\]
  1. 매트랩 명령어 ezplot을 이용해서, \(f\)의 그래프를 그려보세요.
    [사실, ezplot의 상위버전인 fplot라는 명령어가 존재합니다. 하지만, 여기서는 문제의 의도에 맞추어 ezplot를 사용해보도록 하겠습니다.]

  2. 매트랩 명령어 limitplot를 이용해서, (1)에서의 figure에 \(f\)의 수직, 수평 점근선들을 표시해보세요.

  3. 매트랩 명령어 diffsolve를 이용해서, \(f\)의 극소값, 극대값, 변곡점들을 찾아보세요.
    [이 쯤 되면 적분 명령어는 무엇일지 추측해볼 수 있나요?]

  4. 매트랩 명령어 plot을 이용해서, (2)번에서 얻은 figure창에 모든 극점과 변곡점을 표시해보세요.

Solution.

% (i). Plot the given function.
syms x;             % Regard x as a symbol.
num = 3*x^2 + 6*x - 1;
denom = x^2 + x - 3;
f = num / denom;
ezplot(f);          % Plot f(x).
hold on;

% (ii). Draw asymptotes of the given function.
hori_asym = limit(f, inf);                          % Horizontal asymptote.
roots = solve(denom);                               % Vertical asymptote.
plot([-10 10], double(hori_asym)*[1, 1], '--g');    % Draw horizontal asymptote.
plot(double(roots(1))*[1 1], [-100, 100], '--r');   % Draw vertical asymptote.
plot(double(roots(2))*[1 1], [-100, 100], '--r');   % Draw vertical asymptote.

% (iii). Find local maxima, minima, and inflection points.
crit_pts = solve(diff(f, x, 1));    % Solve the equation f'=0 in terms of x.
double(subs(f, crit_pts));          % The local maximum and minimum values of f.
% Solve the equation f''=0 in terms of x.
inflec_pt = solve(diff(f, x, 2), 'real', true);	
double(inflec_pt);                  % Get a number from a symbol.

% (iv). Indicate critical and inflection points.
plot(double(crit_pts), double(subs(f, crit_pts)), 'ro');
plot(double(inflec_pt), double(subs(f, inflec_pt)), 'kd');
xlabel('x'); ylabel('y');

연습문제

  1. 함수 \(f\)와 \(g\)가 다음과 같이 주어졌다고 해보겠습니다.

    \[\begin{align} f(x,\, y) &= e\sin(xy^2) + e\cos(x^2y),\\ g(z) &= \arctan(z). \end{align}\]

    앞선 문제들을 보고 \(f_{xy}(2,\,3)\)와

    \[\int_{-1}^{1} |g(z) - T_{g}^{9}(z)|^2 dz.\]

    의 값을 계산해보세요. 여기서 \(T_{g}^{9}(z)\)는, \(g(z)\)의 \(9\)차 항까지의 테일러 전개를 말합니다.


Symbolic Computation in MATLAB

Assignment 3
Score 0
Submission False

In addition to basic built-in functions of MATLAB, there are many supplementary tool-boxes to be used in a variety of numerical computations. In order to use a specific MATLAB toolbox, you are required to install it separately. Fortunately, KAIST TAH license provides nearly 15 toolboxes which have already installed together with your MATLAB. Among these, we use the Symbolic Math Toolbox which enables us to do symbolic computation in MATLAB.

1. Variables, Factorization and Roots

In this problem, we find the roots of the following polynomial:

\[f(x) = x^6 - 11x^5 + 7x^4 + 163x^3 - 164x^2 - 476x + 480.\]
  1. Using the MATLAB command syms, declare a variable \(x\) as a symbolic object.
  2. Using the MATLAB command factor, find the factors of the given polynomial.
  3. Using the MATLAB command solve, find the roots of the given polynomial.
  4. Using the MATLAB command subs, check that all the roots obtained from item (3) solve the equation \(f(x) = 0\).

Solution.

syms x y;               % Set x, y to be symbols.
f = sin(x^2 * y);
fxx = diff(f, x, 2);    % Differentiate f with respect to x twice.
disp('The 2nd derivative of f with respect to x is'); disp(fxx);

2. Derivative, Substitution and Limit

In this problem, we are doing several calculus problems by symbolic computation.

  1. Using the MATLAB commands diff, find \(f_{xx}\) which is the second derivative of the function \(f\) where

    \[f(x, y) = \sin(x^2y) + \cos(xy^2).\]

    Solution.

     syms x y;               % Set x, y to be symbols.
     f = sin(x^2 * y);
     fxx = diff(f, x, 2);    % Differentiate f with respect to x twice.
     disp('The 2nd derivative of f with respect to x is'); disp(fxx);
    
  2. From the result of (1), using the MATLAB command subs, find \(f_{xx}(1, 2)\). In order to change a symbolic answer to a numeric one, use the MATLAB command double which stands for double precision.

    Solution.

     syms x y;               % Set x, y to be symbols.
     f = sin(x^2 * y);
     fxx = diff(f,x,2);      % Differentiate f with respect to x twice.
     sfxx = simplify(fxx);   % Simplify the expression fxx.
     % Substitute x=1, y=2 into sfxx.
     value = subs(sfxx, [x,y], [1,2]); format rat;
     res = double(value);    % Get a number from a symbol.
     disp('fxx(1,2)='); disp(res);
    
  3. Using the MATLAB command limit, find the following limit

    \[\lim_{h \rightarrow 0} \frac{\cos(x + h) - \cos{x}}{h}\]

    Solution.

     syms x h;           % Set x,h as symbols.
     % Find the limit of the function as h->0.
     res = limit((cos(x+h)-cos(x))/h, h, 0);
     disp('The result of the limit is'); 
     pretty(res);        % Produce a typeset type display.
    

3. Taylor Series and Plotting

In this problem, we plot the Taylor series expansion of the following function:

\[f(x) = \frac{1}{5 + \cos{x}}.\]
  1. Use the MATLAB command taylor to find the Taylor series expansion for \(f(x)\) centered at \(x = 2\) up to the \(10\)th order terms. [Remember that a constant term is \(0\)th order term.]

  2. Use the MATLAB commands fplot and plot(and also hold on) to plot the resulting polynomial of (1) for \(1 \le x \le 3\) together with the given function \(f(x)\) in the same figure.

Solution.

syms x;     % Regard x as a symbol. 
f = 1/(5 + (cos(x)));

% (i). Find the Taylor series expansion of f at x=2 up to the 10th order terms.
T = taylor(f, x, 2, 'order', 11); 

% (ii). Draw the resulting polynomial (i) and the original function f(x).
fplot(T, [1, 3]); hold on;
xd = 1:0.05:3;      yd = subs(f, x, xd);
plot(xd, yd, 'or');
title('Taylor approximation vs actual function');
xlabel('x'); ylabel('y');
legend('Taylor approximation', 'Original function', 'Location', 'best');

4. Local Optima, Inflection Points and Asymptotes

In this problem, we plot the graph and find local maxima, minima, and inflection points of the following function:

\[f(x) = \frac{3x^2 + 6x - 1}{x^2 + x - 3}.\]
  1. Using the MATLAB command ezplot, plot the graph of \(f\).
    [Actually, there is an command fplot which is a upgraded version of ezplot. But let’s use the ezplot to follow the intent of this problem.]

  2. Using the MATLAB commands limit and plot, on the figure from (1), draw the horizontal and vertical asymptotes of \(f\).

  3. Using the MATLAB commands diff and solve, find local maxima, minima and inflection points of \(f\).
    [Can you expect what the integration command is?]

  4. Using the MATLAB commands plot, on the figure from (2), indicate all critical and inflection points of \(f\).

Solution.

% (i). Plot the given function.
syms x;             % Regard x as a symbol.
num = 3*x^2 + 6*x - 1;
denom = x^2 + x - 3;
f = num / denom;
ezplot(f);          % Plot f(x).
hold on;

% (ii). Draw asymptotes of the given function.
hori_asym = limit(f, inf);                          % Horizontal asymptote.
roots = solve(denom);                               % Vertical asymptote.
plot([-10 10], double(hori_asym)*[1, 1], '--g');    % Draw horizontal asymptote.
plot(double(roots(1))*[1 1], [-100, 100], '--r');   % Draw vertical asymptote.
plot(double(roots(2))*[1 1], [-100, 100], '--r');   % Draw vertical asymptote.

% (iii). Find local maxima, minima, and inflection points.
crit_pts = solve(diff(f, x, 1));    % Solve the equation f'=0 in terms of x.
double(subs(f, crit_pts));          % The local maximum and minimum values of f.
% Solve the equation f''=0 in terms of x.
inflec_pt = solve(diff(f, x, 2), 'real', true);	
double(inflec_pt);                  % Get a number from a symbol.

% (iv). Indicate critical and inflection points.
plot(double(crit_pts), double(subs(f, crit_pts)), 'ro');
plot(double(inflec_pt), double(subs(f, inflec_pt)), 'kd');
xlabel('x'); ylabel('y');

Exercises

  1. Let \(f\) and \(g\) be two functions given by

    \[\begin{align} f(x,\, y) &= e\sin(xy^2) + e\cos(x^2y),\\ g(z) &= \arctan(z). \end{align}\]

    Referring to the previous problems compute values of \(f_{xy}(2,\,3)\) and

    \[\int_{-1}^{1} |g(z) - T_{g}^{9}(z)|^2 dz,\]

    where the \(T_{g}^{9}(z)\) denotes \(9\)th order taylor expansion for \(g(z)\).

Leave a comment