Upload
vigambetkar
View
236
Download
1
Embed Size (px)
Citation preview
7/28/2019 Matlab Workshop III.pptx
1/20
Wavez 2011-2012
7/28/2019 Matlab Workshop III.pptx
2/20
Using Functions
Solving Differential Equations
Signal Processing
7/28/2019 Matlab Workshop III.pptx
3/20
Syntax :
function[out1, out2, ] = func_name (input1, input2, )
Note that you need not have any output values, in
which case you simply type:
function function_name (input1, input2, )
7/28/2019 Matlab Workshop III.pptx
4/20
7/28/2019 Matlab Workshop III.pptx
5/20
All functions are defined in a separate .m file.
Each of these .m file names should be the same
as the function name
You can also include sub-functions under each
function (kind of like nested functions)
7/28/2019 Matlab Workshop III.pptx
6/20
There are multiple functions, like ode45, ode23,
bvp4c, and others for solving differential equations
Well only be covering ode45 today, as its a basic
and essential solver.
7/28/2019 Matlab Workshop III.pptx
7/20
ode45 is a solver for an initial-value type of
differential equation.
We need to specify the domain (x-region) in which
we need to solve the equation.
We also need to specify the initial value of the
function at the beginning of the domain.
7/28/2019 Matlab Workshop III.pptx
8/20
The following is an example code to solve y = 3x + 5 , y(0) = 1 on
the domain [0,10] :
function my_solver()
x_dom = [0 10]
y_init = 1;
[X,Y] = ode45( @func, x_dom, y_init);
plot(X,Y);
end
function dy = func(x,y)dy = 3*x + 5;
end
7/28/2019 Matlab Workshop III.pptx
9/20
Now lets try it for the function y = y , y(0) = 1 on the domain [0,10]
:
function my_solver()
x_dom = [0 10]
y_init = 1;
[X,Y] = ode45( @func, x_dom, y_init);
plot(X,Y);
end
function dy = func(x,y)dy = y;
end
7/28/2019 Matlab Workshop III.pptx
10/20
Now lets try it for the functions y1 = y1y2, y2=-3y1 , y1(0) = 1,y2(0)=1 on the domain [0,10] :
function my_solver()x_dom = [0 10]
y_init = [1 1];[X,Y] = ode45( @func, x_dom, y_init);plot(X,Y);
end
function dy = func(x,y)
dy = zeros(2,1);dy(1) = y(1)*y(2);dy(2) = -3*y(1);
end
7/28/2019 Matlab Workshop III.pptx
11/20
Technically, this can only solve for first degree equations.
We can manipulate this to solve higher order equations
in the following way:
For the equation y=-3y, y(0) = 1, y(0) = 1, we
create new functions to represent the higher powers, like
this:
y1 = y
y2= y
7/28/2019 Matlab Workshop III.pptx
12/20
Once again, for y=-3y, y(0) = 0, y(0) = 3:
function my_solver()
x_dom = [0 10]
y_init = [0 3];
[X,Y] = ode45( @func, x_dom, y_init);
plot(X,Y);
end
function dy = func(x,y)
dy(1) = y(2); % y = y2dy(2) = -3*y(1); % y = -3y
end
7/28/2019 Matlab Workshop III.pptx
13/20
Most of our signal processing takes place
in a Matlab toolbox called Simulink.
You can get to Simulink by going to
Matlab Start > Simulink > Library
Browser
7/28/2019 Matlab Workshop III.pptx
14/20
For today, well be dealing with the DataAcquisition toolbox, and the Simulink>Sinksblockset.
The Data Acquisition toolbox allows youaccept analog signals, particularly from yourmicrophone, and save them to a variable
The Sinks blockset allows you to output thisdata in forms that we can read.
7/28/2019 Matlab Workshop III.pptx
15/20
First, from the library, create a new file.
Next, go to Data Acquisition and drag the
Analog Input block into the new file.
Now go to the Sinks blockset, and drag the
To Workspace block into the file.
7/28/2019 Matlab Workshop III.pptx
16/20
Configure the Analog Input block by double-
clicking on it.
For now, check the Asynchronous option, makesure your microphone is selected, set the sample
rate as anything greater than 8000
samples/second, and select only one Hardware
Channel (dont select both)
Once youre finished, close the window.
7/28/2019 Matlab Workshop III.pptx
17/20
Drag an arrow from the Analog Input to the To
Workspace block.
Double-click the To Workspace block to configure it.
Change the name of the variable to which it saves
the data, and set it as a Structure with Time type.
For now, dont change anything else, and close the
window.
7/28/2019 Matlab Workshop III.pptx
18/20
Also, if you want to view your signal, open the Sinks blockset
again, and drag the Scope block into your file.
Pull out an arrow from the Scopes input, and connect it to the
previous line.
Now run the file by pressing the Play button at the top.
Once youve recorded enough, stop the code by pressing the
Stop button.
Double-click on the scope to view your signal. Also, youll find that
this signal has been saved to your workspace in the variable you
mentioned earlier.
7/28/2019 Matlab Workshop III.pptx
19/20
We now use the Fast Fourier Transform
function to find out the frequencies present
in the signal you recorded.
This code will give us the amplitude of
each frequency present in the signal.
7/28/2019 Matlab Workshop III.pptx
20/20
Finito
End
Fin
Graci
as
ByeB
ye