octaveinterpolationoctave-gui

How can I use Octave to return a specific x-axis value given a specific y-axis value?


I've attempted to use the first dimensional interpolate function as well as a spline function to find the value that corresponds to 0dB on a semilogx to decibel graph.

% This calculates the open loop gain of the LM741
clear; clc;
format short eng

f = logspace(-1,7,2E6);  % Frequency range
A0 = 200E3;  % DC gain of LM741
f0 = 4;  % Compensation frequency

A = A0./(1 + j*(f/f0));   % Open loop gain calculation
dB = 20*log10(A);         % Gain to dB conversion
semilogx(f,dB)            % Plot db to f in semilog domain

y0 = 0;                     % Y value for line
line([0.1, 1E7], [y0, y0], 'linestyle','--') % Create horizontal line at y value

xlabel('f [Hz]')
ylabel('A [db]')
title('Open loop gain of LM741')
set(gca,'fontsize',14)
grid on

x0 = abs(spline(dB,f,y0))
x0_2 = abs(interp1(dB,f,y0,'linear'))

The code above is what I sent into octave to generate a graph and an attempted value for f at a value of dB = 0. When I zoom in on the graph the intersection occurs at ~800kHz. However, the output of the values in the console for x0 and x0_2 are as follows:

x0 = 56.9593e+006
x0_2 =  NA

Why would the interpolated spline value be off by such a large margin? And why does the linear interpolation return 'NA'? I'm assuming there's some mathematical feature I'm misunderstanding here. I'm a technical college student who is transferring to university for Electrical Engineering soon, so please bear with my lack of mathematical knowledge.

I'm frankly early enough on into my math journey that many of the concepts in the interpolate function are difficult to understand. I've seen similar answers that involve creating polynomial evaluations that I attempted to replicate, but still failed to get the answer I was looking for.

I assume the answer is in the features of polynomial fitting and evaluation, but I have read all of the corresponding documentation and still can not understand the steps to solve this answer.


Solution

  • Your stated objective is to find the value of f for which abs(dB(f)) = 0, but your calculation finds abs(dB(0)), which is not the same thing. You have substituted f = 0 but what you need to do is solve for f. You can do that using the fsolve() function. Interpolation is not required. The following code

    A0 = 200E3;
    f0 = 4;
    y0 = 0;
    
    function y = dB(f, A0, f0)  
        A = A0./(1 + j*(f/f0));   
        y = abs(20*log10(A));
    endfunction
    
    a = @(f) dB(f, A0, f0) - y0;
    
    y = fsolve(a, 1e6)
    

    prints out y = 799.8047e+003. The argument 1e6 passed to the fsolve function is an initial "guess" from which the function homes in to the solution.