# Dynamics/Linearization/Numerical Solutions/Single Variable with MATLAB

## Introduction

MATLAB Scripts can be simple and straightforward in linearizing and calculating a numerical solution to a non-linear expression of a single variable.

## Using a MATLAB Script

The first two terms of the Taylor Series expansion, or linear approximation, result in the following:

${\displaystyle f(x)\approx f(a)+f'(a)(x-a)}$

Using MATLAB, there are a few options for performing linear approximations. Using this resource, we can run the following script to linear the following expression:

${\displaystyle f(x)=x^{2}+\sin(x)+1}$
Code
clear all
close all

syms x a epsilon
f=(x.^2+sin(x)+1)
f_lin=subs(f,a)+subs(diff(f,x),a)*(x-a)
a=1;
f_lin_a_set=subs(f_lin);
simplify(subs(f_lin))

%An alternative technique for Tayor Series Expansion
%simplify(taylor(f,x,'ExpansionPoint',a,'order',2))

%Plotting f and the linearized solution

fplot([f f_lin_a_set],[0,3],'linewidth',3)
legend('f','Linearized f','Location','SouthEast')
xlabel('x','FontSize',12)
ylabel('f(x)','FontSize',12)

%Substitute  for

x=a+epsilon;
simplify(subs(f_lin))


The figure shows a resulting depiction of the linearized function.

The linearized function near ${\displaystyle x=1}$ is the following:

${\displaystyle f(a)\approx \sin(1)+\epsilon (\cos(1)+2)+2}$

We can substitute ${\displaystyle t}$ for ${\displaystyle x}$ in the expression:

${\displaystyle f(x)=x^{2}+\sin(x)+1}$

Then, we create an appropriate block diagram as shown in the Figure.

We can then select the blocks between the ramp and the scope, right-click to create a subsystem block, select the new block, right-click an select "Linear Analysis", specify a point for linearization (${\displaystyle t=1}$ in this case), linearize, and look at the resulting linear analysis in terms of state-space representation.

For our example, we find the "Model Linearizer" gives a static gain of 2.54 for our subsystem block.

## Individual or Group Activity

• Write down your own individual non-linear expression.
• Use Taylor or binomial series expansion to create an analytical expression for the linearized form of your expression at a specified location.
• Calculate the slope at that specified location.
• Write a MATLAB script to create a symbolic linearized expression at the specified location.
• Use Simulink to calculate the slope at the specified location.
• Create a presentation in Google Slides titled "Linearized Expression with a Single Variable" to share in 2 minutes with the class.