Modelling and Simulation in MATLAB - Overview
Transcript of Modelling and Simulation in MATLAB - Overview
![Page 1: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/1.jpg)
Modelling,Simulation&Control
Hans-PetterHalvorsen,M.Sc.
QuickStartTutorial
![Page 2: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/2.jpg)
WhatisMATLAB?• MATLABisatoolfortechnicalcomputing,computationandvisualizationinanintegratedenvironment.
• MATLABisanabbreviationforMATrixLABoratory• ItiswellsuitedforMatrix manipulationandproblemsolvingrelatedtoLinearAlgebra,Modelling,SimulationandControlApplications
• PopularinUniversities, TeachingandResearch
![Page 3: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/3.jpg)
Lessons1. SolvingDifferentialEquations(ODEs)2. DiscreteSystems3. Interpolation/CurveFitting4. NumericalDifferentiation/Integration5. Optimization6. TransferFunctions/State-spaceModels7. FrequencyResponse
![Page 4: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/4.jpg)
Lesson1Solving ODEsinMATLAB- OrdinaryDifferentialEquations
![Page 5: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/5.jpg)
DifferentialEquationsExample:
Note!
TheSolutioncanbeprovedtobe(willnotbeshownhere):
T = 5;a = -1/T;x0 = 1;t = [0:1:25];
x = exp(a*t)*x0;
plot(t,x);grid
Students:Trythisexample
Usethefollowing:
Where
TistheTimeconstant
![Page 6: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/6.jpg)
DifferentialEquations
T = 5;a = -1/T;x0 = 1;t = [0:1:25];
x = exp(a*t)*x0;
plot(t,x);grid
Problemwiththismethod:WeneedtosolvetheODEbeforewecanplotit!!
![Page 7: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/7.jpg)
UsingODESolversinMATLABExample:Step1:DefinethedifferentialequationasaMATLABfunction(mydiff.m):
Step2:Useoneofthebuilt-inODEsolver(ode23,ode45,...)inaScript.
Students:Trythisexample.Doyougetthesameresult?
function dx = mydiff(t,x) T = 5;a = -1/T;dx = a*x;
clearclc
tspan = [0 25];x0 = 1;
[t,x] = ode23(@mydiff,tspan,x0);plot(t,x)
tspan
x0
![Page 8: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/8.jpg)
HigherOrderODEs
InordertousetheODEsinMATLABweneedreformulateahigherordersystemintoasystemoffirstorderdifferentialequations
Mass-Spring-DamperSystem
Example(2.orderdifferentialequation):
![Page 9: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/9.jpg)
HigherOrderODEs
InordertousetheODEsinMATLABweneedreformulateahigherordersystemintoasystemoffirstorderdifferentialequations
Mass-Spring-DamperSystem:
Weset: Thisgives: Finally:
NowwearereadytosolvethesystemusingMATLAB
![Page 10: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/10.jpg)
Step1:DefinethedifferentialequationasaMATLABfunction(mass_spring_damper_diff.m):
Step2:Usethebuilt-inODEsolverinascript.
Students:Trythisexample
function dx = mass_spring_damper_diff(t,x)
k = 1;m = 5;c = 1;F = 1;
dx = zeros(2,1); %Initialization
dx(1) = x(2);dx(2) = -(k/m)*x(1)-(c/m)*x(2)+(1/m)*F;
clearclc
tspan = [0 50];x0 = [0;0];
[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);plot(t,x)
Students:Trywithdifferentvaluesfork,m,candF
![Page 11: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/11.jpg)
...[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);plot(t,x)
...[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);plot(t,x(:,2))
![Page 12: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/12.jpg)
function dx = mass_spring_damper_diff(t,x, param)
k = param(1);m = param(2);c = param(3);F = param(4);
dx = zeros(2,1);
dx(1) = x(2);dx(2) = -(k/m)*x(1) - (c/m)*x(2) + (1/m)*F;
Step1:DefinethedifferentialequationasaMATLABfunction(mass_spring_damper_diff.m):
Students:Trythisexample
Forgreaterflexibilitywewanttobeabletochangetheparametersk,m,c,andFwithoutchangingthefunction,onlychangingthescript.Abetterapproachwouldbetopasstheseparameterstothefunctioninstead.
![Page 13: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/13.jpg)
clearclcclose all
tspan = [0 50];x0 = [0;0];
k = 1;m = 5;c = 1;F = 1;param = [k, m, c, F];
[t,x] = ode23(@mass_spring_damper_diff,tspan,x0, [], param);plot(t,x)
Step2:Usethebuilt-inODEsolverinascript:
Students:Trythisexample
![Page 14: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/14.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 15: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/15.jpg)
Lesson 2
• DiscreteSystems
![Page 16: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/16.jpg)
DiscreteSystems
• Whendealingwithcomputersimulations,weneedtocreateadiscreteversionofoursystem.
• Thismeansweneedtomakeadiscreteversionofourcontinuousdifferentialequations.
• Actually,thebuilt-inODEsolversinMATLABusedifferentdiscretizationmethods
• Interpolation,CurveFitting,etc.isalsobasedonasetofdiscretevalues(datapointsormeasurements)
• ThesamewithNumericalDifferentiationandNumericalIntegration
• etc.
x y
0 15
1 10
2 9
3 6
4 2
5 0
Discretevalues
![Page 17: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/17.jpg)
DiscreteApproximationofthetimederivativeEulerbackwardmethod:
Eulerforwardmethod:
DiscreteSystems
![Page 18: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/18.jpg)
DiscretizationMethods
Eulerforwardmethod:
Eulerbackwardmethod:
OthermethodsareZeroOrderHold(ZOH),Tustin’smethod,etc.
Simplertouse!
MoreAccurate!
DiscreteSystems
![Page 19: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/19.jpg)
DifferentDiscreteSymbolsandmeanings
Present Value:
Previous Value:
Next (Future)Value:
Note!DifferentNotationisusedindifferentlitterature!
DiscreteSystems
![Page 20: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/20.jpg)
DiscreteSystems
Giventhefollowingcontinuoussystem(differentialequation):
WewillusetheEulerforwardmethod:
Example:
Students:Findthediscretedifferentialequation(penandpaper)andthensimulatethesysteminMATLAB,i.e.,plottheStepResponseofthesystem.Tip!Useaforloop
WhereumaybetheControlSignalfrome.g.,aPIDController
![Page 21: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/21.jpg)
DiscreteSystemsSolution:
Giventhefollowingcontinuoussystem:
WewillusetheEulerforwardmethod:
![Page 22: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/22.jpg)
DiscreteSystemsSolution:
% Simulation of discrete modelclear, clc, close all
% Model Parametersa = 0.25;b = 2;
% Simulation ParametersTs = 0.1; %sTstop = 20; %suk = 1; % Step in ux(1) = 0; % Initial value
% Simulationfor k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;end
% Plot the Simulation Resultsk=0:Ts:Tstop;plot(k, x)grid on
MATLABCode:
Students:Trythisexample
Students:Analternativesolutionistousethebuilt-infunctionc2d() (convertfromcontinoustodiscrete).Trythisfunctionandseeifyougetthesameresults.
![Page 23: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/23.jpg)
DiscreteSystemsSolution:
% Find Discrete modelclear, clc, close all
% Model Parametersa = 0.25;b = 2;Ts = 0.1; %s
% State-space modelA = [-a];B = [b];C = [1];D = [0];
model = ss(A,B,C,D)model_discrete = c2d(model, Ts, 'forward')step(model_discrete)grid on
MATLABCode:
Students:Trythisexample
EulerForwardmethod
![Page 24: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/24.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 25: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/25.jpg)
Lesson 3
• Interpolation• CurveFitting
![Page 26: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/26.jpg)
Interpolationx y
0 15
1 10
2 9
3 6
4 2
5 0
GiventhefollowingDataPoints:
Problem:Wewanttofindtheinterpolatedvaluefor,e.g.,𝑥 = 3.5
Example
Students:Trythisexample
x=0:5;y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'o')grid
(LoggedDatafromagivenProcess)
![Page 27: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/27.jpg)
Interpolation
x=0:5;y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'-o')grid on
new_x=3.5;new_y = interp1(x,y,new_x)
new_y =
4
Wecanuseoneofthebuilt-inInterpolationfunctionsinMATLAB:
MATLABgivesustheanswer4.Fromtheplotweseethisisagoodguess:
Students:Trythisexample
![Page 28: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/28.jpg)
CurveFitting
• Intheprevioussectionwefoundinterpolatedpoints,i.e.,wefoundvaluesbetweenthemeasuredpointsusingtheinterpolationtechnique.
• Itwouldbemoreconvenienttomodelthedataasamathematicalfunction𝑦 = 𝑓(𝑥).
• Thenwecan easilycalculateanydatawewantbasedonthismodel.
DataMathematicalModel
![Page 29: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/29.jpg)
CurveFitting LinearRegression
x y
0 15
1 10
2 9
3 6
4 2
5 0
GiventhefollowingDataPoints:
Example:
x=0:5;y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'o')grid
Basedontheplotweassumealinearrelationship:
WewilluseMATLABinordertofindaandb.
BasedontheDataPointswecreateaPlotinMATLAB
Students:Trythisexample
![Page 30: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/30.jpg)
CurveFitting LinearRegressionExample
Basedontheplotweassumealinearrelationship:
WewilluseMATLABinordertofindaandb.
clearclc
x=[0, 1, 2, 3, 4 ,5];y=[15, 10, 9, 6, 2 ,0];n=1; % 1.order polynomialp = polyfit(x,y,n)
p =
-2.9143 14.2857
Next:WewillthenplotandvalidatetheresultsinMATLAB
Students:Trythisexample
![Page 31: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/31.jpg)
CurveFitting LinearRegressionExample
clearclcclose all
x=[0, 1, 2, 3, 4 ,5];y=[15, 10, 9, 6, 2 ,0];n=1; % 1.order polynomialp=polyfit(x,y,n);
a=p(1);b=p(2);
ymodel = a*x+b;
plot(x,y,'o',x,ymodel)Students:Trythisexample
WewillplotandvalidatetheresultsinMATLAB
x y
0 15
1 10
2 9
3 6
4 2
5 0
Weseethisgivesagoodmodelbasedonthedataavailable.
![Page 32: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/32.jpg)
CurveFitting LinearRegression
Students:Trythisexample
Problem:Wewanttofindtheinterpolatedvaluefor,e.g.,x=3.5
3waystodothis:• Usetheinterp1 function (shownearlier)• Implementy=-2.9+14.3 andcalculatey(3.5)• Usethepolyval function
x y
0 15
1 10
2 9
3 6
4 2
5 0
... (see previus examples)
new_x=3.5;
new_y = interp1(x,y,new_x)
new_y = a*new_x + b
new_y = polyval(p, new_x)
![Page 33: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/33.jpg)
CurveFitting PolynomialRegression
Students:Trytofindmodelsbasedonthegivendatausingdifferentorders(1.order– 6.ordermodels).Plotthedifferentmodelsinasubplotforeasycomparison.
1.order:
2.order:
3.order:
etc.
p = polyfit(x,y,1)
x y
0 15
1 10
2 9
3 6
4 2
5 0
p = polyfit(x,y,2)
p = polyfit(x,y,3)
![Page 34: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/34.jpg)
clearclcclose all
x=[0, 1, 2, 3, 4 ,5];y=[15, 10, 9, 6, 2 ,0];
for n=1:6 % n = model order
p = polyfit(x,y,n)
ymodel = polyval(p,x);
subplot(3,2,n)plot(x,y,'o',x,ymodel)title(sprintf('Model order %d', n));
end
CurveFitting
• Asexpected,thehigherordermodelsmatchthedatabetterandbetter.
• Note!Thefifthordermodelmatchesexactlybecausetherewereonlysixdatapointsavailable.
• n>5makesnosensebecausewehaveonly6datapoints
![Page 35: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/35.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 36: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/36.jpg)
Lesson 4
• NumericalDifferentiation• NumericalIntegration
![Page 37: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/37.jpg)
NumericalDifferentiation
Anumericalapproachtothederivativeofafunctiony=f(x)is:
Note!WewilluseMATLABinordertofindthenumeric solution– nottheanalyticsolution
![Page 38: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/38.jpg)
NumericalDifferentiationExample:
Weknowforthissimpleexamplethattheexactanalyticalsolutionis:
Giventhefollowingvalues:x y
-2 4
-1 1
0 0
1 1
2 4
![Page 39: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/39.jpg)
NumericalDifferentiationExample:
MATLABCode:x=-2:2;y=x.^2;
% Exact Solutiondydx_exact = 2*x;
% Numerical Solutiondydx_num = diff(y)./diff(x);
% Compare the Resultsdydx = [[dydx_num, NaN]', dydx_exact']plot(x,[dydx_num, NaN]', x, dydx_exact')
Students:Trythisexample.Tryalsotoincreasenumberofdatapoints,x=-2:0.1:2
dydx =
-3 -4-1 -21 03 2
NaN 4
ExactSolution
NumericalSolution
numeric
exact
![Page 40: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/40.jpg)
NumericalDifferentiation
x=-2:2
x=-2:0.1:2
Theresultsbecomemoreaccuratewhenincreasingnumberofdatapoints
![Page 41: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/41.jpg)
NumericalIntegration
Anintegralcanbeseenastheareaunderacurve.Giveny=f(x) theapproximationoftheArea(A)underthecurvecanbefounddividingtheareaupintorectanglesandthensummingthecontributionfromalltherectangles(trapezoidrule):
![Page 42: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/42.jpg)
NumericalIntegrationExample:Weknowthattheexactsolutionis:
x=0:0.1:1;y=x.^2;plot(x,y)
% Calculate the Integral:avg_y=y(1:length(x)-1)+diff(y)/2;A=sum(diff(x).*avg_y)
WeuseMATLAB(trapezoidrule):
A = 0.3350Students:Trythisexample
![Page 43: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/43.jpg)
NumericalIntegrationExample:Weknowthattheexactsolutionis:
clearclcclose all
x=0:0.1:1;y=x.^2;
plot(x,y)
% Calculate the Integral (Trapezoid method):avg_y = y(1:length(x)-1) + diff(y)/2;A = sum(diff(x).*avg_y)
% Calculate the Integral (Simpson method):A = quad('x.^2', 0,1)
% Calculate the Integral (Lobatto method):A = quadl('x.^2', 0,1)
Students:Trythisexample.Comparetheresults.Which givesthebestmethod?
InMATLABwehaveseveralbuilt-infunctionswecanusefornumericalintegration:
![Page 44: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/44.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 45: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/45.jpg)
Lesson 5
• Optimization
![Page 46: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/46.jpg)
OptimizationOptimizationisimportantinmodelling,controlandsimulationapplications.Optimizationisbasedonfindingtheminimumofagivencriteriafunction.
WewanttofindforwhatvalueofxthefunctionhasitsminimumvalueExample:
clearclc
x = -20:0.1:20;y = 2.*x.^2 + 20.*x - 22;plot(x,y)grid
i=1;while ( y(i) > y(i+1) )
i = i + 1; end
x(i)y(i)
Students:Trythisexample
Theminimumofthefunction
(-5,72)
![Page 47: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/47.jpg)
OptimizationExample:
clearclcclose all
x = -20:1:20;f = mysimplefunc(x);plot(x, f)grid
x_min = fminbnd(@mysimplefunc, -20, 20)
y = mysimplefunc(x_min)
Students:Trythisexample
function f = mysimplefunc(x)
f = 2*x.^2 + 20.*x -22;
x_min =
-5
y =
-72
Note!ifwehavemorethan1variable,wehavetousee.g.,thefminsearch function
Wegotthesameresultsaspreviousslide
![Page 48: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/48.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 49: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/49.jpg)
Lesson 6
• TransferFunctions• State-spacemodels
![Page 50: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/50.jpg)
Transferfunctions
TransferFunctions
DifferentialEquations
Laplace
H(s)
Example:
Input Output
Numerator
Denumerator
ATransferfunctionistheratiobetweentheinputandtheoutputofadynamicsystemwhenalltheothersinputvariablesandinitialconditionsissettozero
![Page 51: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/51.jpg)
Transferfunctions
1.orderTransferfunction:
StepResponse:
1.orderTransferfunctionwithTimeDelay:
![Page 52: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/52.jpg)
TransferfunctionsExample:
clearclcclose all
% Transfer Functionnum = [4];den = [2, 1];H = tf(num, den)
% Step Responsestep(H)
MATLAB:
Students:Trythisexample
![Page 53: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/53.jpg)
Transferfunctions2.orderTransferfunction:
Example:
clearclcclose all
% Transfer Functionnum = [2];den = [1, 4, 3];H = tf(num, den)
% Step Responsestep(H)
Students:Trythisexample.TrywithdifferentvaluesforK,a,b andc.
MATLAB:
![Page 54: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/54.jpg)
State-spacemodelsAsetwithlineardifferentialequations:
Canbestructuredlikethis:
Whichcanbestatedonthefollowingcompactform:
![Page 55: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/55.jpg)
State-spacemodelsExample:
clearclcclose all
% State-space modelA = [1, 2; 3, 4];B = [0; 1];
C = [1, 0];D = [0];
ssmodel = ss(A, B, C, D)
% Step Responsestep(ssmodel)
% Transfer functionH = tf(ssmodel)
Students:Trythisexample
MATLAB:Note!Thesystemisunstable
![Page 56: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/56.jpg)
Mass-Spring-DamperSystem
Example:
State-spacemodels
Students:FindtheState-spacemodelandfindthestepresponseinMATLAB.Trywithdifferentvaluesfork,m,candF.Discusstheresults
![Page 57: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/57.jpg)
Mass-Spring-DamperSystemState-spacemodelsWeset: Thisgives:
Finally:
Thisgives:
Note!wehavesetF=u
k = 5;c = 1;m = 1;
A = [0 1; -k/m -c/m];B = [0; 1/m];C = [0 1];D = [0];sys = ss(A, B, C, D)
step(sys)
![Page 58: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/58.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 59: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/59.jpg)
Lesson 7
• FrequencyResponse
![Page 60: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/60.jpg)
FrequencyResponse
DynamicSystem
Imput SignalOutputSignal
Gain PhaseLag
AirHeater
FrequencyAmplitude
Thefrequencyresponseofasystemexpresseshowasinusoidalsignalofagivenfrequencyonthesysteminputistransferredthroughthesystem.
Example:
![Page 61: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/61.jpg)
FrequencyResponse- Definition
• Thefrequencyresponseofasystemisdefinedasthesteady-state responseofthesystemtoasinusoidalinputsignal.
• Whenthesystemisinsteady-state,itdiffersfromtheinputsignalonlyinamplitude/gain(A)(“forsterkning”)andphaselag(ϕ)(“faseforskyvning”).
andthesameforFrequency3,4,5,6,etc.
![Page 62: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/62.jpg)
FrequencyResponseExample:
clearclcclose all
% Define Transfer functionnum=[1];den=[1, 1];H = tf(num, den)
% Frequency Responsebode(H);grid on Thefrequencyresponseisanimportanttoolforanalysisanddesign
ofsignalfiltersandforanalysisanddesignofcontrolsystems.
Students:TrythisExample
![Page 63: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/63.jpg)
Whatsnext?
Self-pacedTutorialswithlotsofExercisesandVideoresources
DoasmanyExercisesaspossible! TheonlywaytolearnMATLABisbydoingExercisesandhands-onCoding!!!
LearningbyDoing!
![Page 64: Modelling and Simulation in MATLAB - Overview](https://reader034.fdocuments.net/reader034/viewer/2022051521/586ccf8d1a28abf6518bad2f/html5/thumbnails/64.jpg)
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/