How can I return the maximum value of a bodemag minus a linear approximation if I have both systems plotted?

11 次查看(过去 30 天)
I have a transfer function and its linear approximation graphed, and I would like to return the frequency at which the bodemag graph and linear approximation are the most different from each other and also the dB difference.
I think that this can either be done by finding an equation for the transfer function that exactly describes how it behaves, and then just subtract the linear function and find it that way, or have a way to look at the points on the graph and find the x axis value where the y axis difference is the biggest on a chosen interval.
In my head it sounds like the latter would be easier, but I don't know how to execute it.
Attached below is an image describing what I would like to know and my graphing code.
orig = tf([2500],[1 20 2500]);
syms x;
linApprox = piecewise(x<50, 0, x>50, -40*log10(x/50));
fplot(linApprox,'r')
hold on
bodemag(orig,'b')

采纳的回答

Ayush
Ayush 2023-10-5
Hi Robert,
I understand that you are trying to determine the maximum deviation in magnitude (in dB) and the corresponding frequency at which the two curves, bodemag and linear approximation, exhibit the greatest difference.
To do so:
  1. you can specify the frequency range of the curve, and as per your given plot range can be written as: “frequencies = logspace(-2, 3, 1000);”
  2. Then substitute these frequencies in the “orig” curve, which can be done by using: “origMagnitude = abs(freqresp(orig, frequencies));”
  3. This will return the respective magnitude of the curve and to convert the curve in dB, use “20*log10(origMagnitude)”.
  4. Similarly substitute the frequency value is the “linApprox” curve using: “linApproxMagnitude = subs(linApprox, x, frequencies);”
  5. Finally find deviation between the two curves using the following equation: “deviation = squeeze(20*log10(origMagnitude))'-squeeze(double(linApproxMagnitude));”
  6. To find the maximum value of all the deviations calculated, use the “max()” function.
Final code snippet is given below:
orig = tf([2500],[1 20 2500]);
syms x;
linApprox = piecewise(x<50, 0, x>50, -40*log10(x/50));
fplot(linApprox,'r')
hold on
bodemag(orig,'b')
%%
frequencies = logspace(-2, 3, 1000); % Adjust the range and number of frequencies as needed
% Calculate the magnitude response of the transfer function
origMagnitude = abs(freqresp(orig, frequencies));
% Calculate the magnitude response of the piecewise function
linApproxMagnitude = subs(linApprox, x, frequencies);
% Calculate the deviation between the two magnitude responses
deviation = squeeze(20*log10(origMagnitude))' - squeeze(double(linApproxMagnitude));
% Find the maximum deviation and its corresponding frequency
[maxDeviation, maxDeviationIndex] = max(abs(deviation));
maxDeviationFrequency = frequencies(maxDeviationIndex);
% Display the results
disp("Maximum deviation in magnitude frequency response:");
Maximum deviation in magnitude frequency response:
disp("Deviation: " + maxDeviation);
Deviation: 8.1347
disp("Frequency: " + maxDeviationFrequency);
Frequency: 52.3261
Hope this helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by