help-octave
[Top][All Lists]

Re: fsolve() not adjusting all function variables

 From: Tatsuro MATSUOKA Subject: Re: fsolve() not adjusting all function variables Date: Tue, 26 Aug 2014 18:18:30 +0900 (JST)

```----- Original Message -----

> From: gerritb
> Cc:
> Date: 2014/8/26, Tue 04:24
> Subject: fsolve() not adjusting all function variables
>
> I am an Octave novice and a new user of fsolve(), which I am using to fit a
> formula to complex data.  I am generating a set of complex electrical
> impedance data here (phase shift caused by capacitance C and inductance L)
> and I want to fit an impedance formula to it.  My problem is that fsolve()
> seems to adjust only some of the input variables to the function 'fit'.
> In
> the example below it does not change my initial guess of R (100), even
> though changing it would result in a better fit to the data.  What am I
> doing wrong?
>
> Many thanks,
> Gerrit
> ---------------------------------------------------
> clear all; clf;
> % To enable use of 'fsolve':
>
> function F = Zl (L, f)
>   F = j*2*pi*f*L;
> endfunction
>
> function F = Zc (C, f)
>   F = 1./(j*2*pi*f*C);
> endfunction
>
> % Define a function to drive to zero. Input arg is the independent vars.
> function F = fit(vin)
>   global f;
>   global Vc;
>   % Can redefine independent vars for more meaningful names
>   R = vin(1);
>   C = vin(2);
>   L = vin(3);
>   % Return function evaluation at input vin, column vector.
>   % Arrange so function = zero (ie. subtract eqn left side from right)
>   F = Vc - (Zc(C, f) ./ (R + Zl(L, f) + Zc(C, f)));
>   % Separate real/imag columns so both are driven to zero
>   F = [real(F), imag(F)];
> endfunction
>
> % This section creates a noisy curve to fit the function to: ========
> % Here are the pre-noise exact values which fsolve will approximate:
> R = 50;
> C = 1e-9;
> L = 1e-5;
> % Store input f and Z as two separate vectors
> % Column vector of f values
> global f = logspace(4, 8, 100)';
> % Create a noisy complex curve in Vc
> global Vc = Zc(C, f) ./ (R + Zl(L, f) + Zc(C, f));
> rn = real(Vc) .* (1 + .05*randn(size(f)));
> in = imag(Vc) .* (1 + .05*randn(size(f)));
> Vc = rn + j*in;
> % ===================================================================
>
> % Initial solution guess (column vector)
> vin = [100; 2e-9; 1.2e-5];
> % Display, TolFun, and TolX options don't seem to have an effect:
> % options = optimset('Display', 'iter', 'TolFun',
> 1e-12);
> [vout, fval, info, output]=fsolve('fit', vin);
>
> % Print out the results
> % 'vout' is the solution vector
> % 'fval' is the final function evaluation (can be used to evaluate how
> %    good the fit is)
> % 'info = 1' means solution converged, but 2 and 3 seem to be okay too
> % 'output' lists some statistics about the process
> vout
> info
> output
>
> % Plot input data & best fit curve
> fitVc = Zc(vout(2), f) ./ (vout(1) + Zl(vout(3), f) + Zc(vout(2), f));
> inVc = Vc;
> subplot(2, 1, 1);
> hold;
> semilogx (f, abs(inVc), 'r.');
> semilogx (f, abs(fitVc), 'b');
> subplot(2, 1, 2);
> hold;
> semilogx (f, arg(inVc)*180/pi, 'r.');
> semilogx (f, arg(fitVc)*180/pi, 'b');
>
>

% To enable use of 'fsolve':

What version octave do you use?
To my knowledge, fsolve has not been a function in the optim package but but a
core function of octave.

Therefore pkg load optim is not required.

Tatsuro

```