How to Simulate and Plot a Transfer Function in MATLAB?

In summary: XYandTF(x, y)subplot(1, 2, 1);plot(t, x); hold on;plot(t, y); hold off;num = K * (s-a); % define numeratorden = (s-b)*(s-c); % define denominatorsys = tf(num, den); % convert transfer function to objectsubplot(1,2,2);bode(sys); % plot Bode plotendIn summary, to simulate the system defined by Y(s) = H(s)X(s) with H(s) = K \frac{s-a}{(s-b)(s-c)}, you would need to create two functions. The first function would simulate the system for given values of a,
  • #1
XcKyle93
37
0

Homework Statement



Consider the system defined by Y(s) = H(s)X(s) with H(s) = K [itex]\frac{s-a}{(s-b)(s-c)}[/itex]. Build a function in MATLAB to simulate this system for given values of a,b,c,K and various input signals x(t) (in the time or in s-domain). Include a function to display plots of the time functions x and y and the Bode plot of the transfer function. You can choose b,c so that the system is stable (that is, both b,c are positive).

Homework Equations


The Attempt at a Solution


I believe that the question is asking for two different functions. Here's my attempt at the first function:

function simButter(a, b, c, K, x)
syms s
sys = K * (s-a)/((s-b)*(s-c))

dt = 0.01;
t = 0:dt:10; %dt and 10 were arbitrarily picked; what else would I choose?

lsim(sys, x(t), t)

end

For the second function, I have:
function plotXYandTF(x,y)
dt = 0.01;
t = 0:dt:10;

subplot(1, 2, 1);
plot(t, x(t)); hold on;
plot(t, y(t)); hold off;

sys = tf(x,y);
subplot(1,2,2);
bode(sys)
end

Is this even right? I am kind of confused about the way the question is phrased. In general, I know very little about filtering with MATLAB, despite reading a lot of the docs on mathworks.
 
Physics news on Phys.org
  • #2


Hello,

Thank you for posting this question on the forum. As a fellow scientist, I understand the importance of being able to accurately simulate and analyze systems in MATLAB. I have reviewed your attempt at the solution and have some suggestions that may help you.

Firstly, your attempt at the first function is on the right track. However, there are a few things that need to be adjusted. Firstly, you do not need to declare the variable "s" as a symbolic variable since you are using the "lsim" function, which is meant for continuous-time systems. Therefore, I would recommend removing the "syms s" line from your code.

Secondly, you have correctly defined the system transfer function, but you have not defined the input signal "x(t)". This can be done by creating a vector of values for the input signal, corresponding to the time vector "t". For example, if you want a sinusoidal input signal, you could define "x" as "sin(t)".

Lastly, the "lsim" function requires the system transfer function to be in the form of a transfer function object. Therefore, I would recommend converting the symbolic expression "sys" to a transfer function object using the "tf" function. The updated code for the first function would look something like this:

function simButter(a, b, c, K, x)

dt = 0.01;
t = 0:dt:10;

sys = tf(K * (s-a)/((s-b)*(s-c))); % convert transfer function to object
x = sin(t); % define input signal
y = lsim(sys, x, t); % simulate system

end

For the second function, you are on the right track as well. However, there are a few things that need to be adjusted. Firstly, you do not need to define the time vector "t" and input signal "x" again, as they are already defined in the first function. Therefore, you can remove those lines from your code. Secondly, you need to define the output signal "y" as well, which is the result of the "lsim" function in the first function. Lastly, the "tf" function requires two arguments, the numerator and denominator of the transfer function. Therefore, you would need to define the numerator and denominator separately before using the "tf" function. The updated code for the second function would look something like this:

function plot
 

FAQ: How to Simulate and Plot a Transfer Function in MATLAB?

What is MATLAB Filtering Signals and why is it important?

MATLAB Filtering Signals is a tool used in signal processing to remove unwanted noise or interference from a signal, allowing for a clearer and more accurate representation of the original signal. It is important because it helps improve the quality and reliability of data analysis and interpretation in various fields such as engineering, medicine, and finance.

What are the different types of filters available in MATLAB?

MATLAB offers a wide range of filters, including low-pass, high-pass, band-pass, and band-stop filters. These filters can be designed using different methods such as finite impulse response (FIR) and infinite impulse response (IIR) filters, and can be applied to continuous or discrete signals.

How do I design a filter in MATLAB?

To design a filter in MATLAB, you can use the built-in functions such as fdesign or designfilt, which allow you to specify the filter type, order, and cutoff frequencies. You can also use the Filter Design and Analysis Tool to interactively design and analyze filters.

Can I apply multiple filters to a signal in MATLAB?

Yes, you can apply multiple filters to a signal in MATLAB by cascading them using the cascade function. This allows you to combine different types of filters to achieve a desired frequency response.

How do I evaluate the effectiveness of a filter in MATLAB?

There are several metrics that can be used to evaluate the effectiveness of a filter in MATLAB, such as the magnitude and phase response, group delay, and stopband attenuation. You can also compare the filtered signal to the original signal using measures like signal-to-noise ratio (SNR) or root mean square error (RMSE).

Similar threads

Replies
10
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
6
Views
4K
Replies
1
Views
3K
Replies
6
Views
2K
Replies
3
Views
1K
Replies
4
Views
4K
Back
Top