0

I am quite new to Matlab and I am trying to use this code I found online. I am trying to fit a graph described by the HydrodynamicSpectrum. But instead of having it fit after inputting fvA and fmA, I am trying to obtain the fitted parameters for this value also.

I have tried removing them, changing them. But none is working. I was wondering if any one here will be able to point me into the right direction of fixing this.

specFunc = @(f, para)HydrodynamicSpectrum(f, [para fvA fmA]);
[fit.AXfc, fit.AXD] = NonLinearFit(fit.f(indXY), fit.AXSpec(indXY), specFunc, [iguess_AXfc iguess_AXD]);
[fit.AYfc, fit.AYD] = NonLinearFit(fit.f(indXY), fit.AYSpec(indXY), specFunc, [iguess_AYfc iguess_AYD]);
[fit.ASumfc, fit.ASumD] = NonLinearFit(fit.f(indSum), fit.ASumSpec(indSum), specFunc, [iguess_ASumfc iguess_ASumD]);
predictedAX = HydrodynamicSpectrum(fit.f, [fit.AXfc fit.AXD fvA fmA]);
predictedAY = HydrodynamicSpectrum(fit.f, [fit.AYfc fit.AYD fvA fmA]);
predictedASum = HydrodynamicSpectrum(fit.f, [fit.ASumfc fit.ASumD fvA fmA]);

function spec = HydrodynamicSpectrum(f, para);

fc = para(1);
D = para(2);
fv = para(3);
fm = para(4);
f = abs(f);  %Kludge!
spec = D/pi^2*(1+sqrt(f/fv))./((fc - f.*sqrt(f./fv) - (f.^2)/fm).^2 + (f + f.*sqrt(f./fv)).^2);


function [fc, D, sfc, sD] = NonLinearFit(f, spec, specFunc, init);

func = @(para, f)spec./specFunc(f, para);

[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);

fc = paraFit(1);
D = paraFit(2);

ci = nlparci(real(paraFit), real(resid), real(J));  % Kludge!!

sfc = (ci(1,2) - ci(1,1))/4;
sD = (ci(2,2) - ci(2,1))/4;
palansuya
  • 7
  • 3

1 Answers1

0
[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);

It looks like you get your fitted parameter using this line. And you are further processing them to get other stuff out from the function. You can modify your second function to get them out as well.

As there are very few comments, and questions seems to be application specific, there is not much help I can give with what you have presented.

Kandel
  • 46
  • 1