0
$\begingroup$

I'm trying to fit my data to function A.

z = 50;
s[x_] == (((q^2 (x/wr - wr/x)^2 + (z/(2 r) + 1))/(q^2 (x/wr - wr/x)^2 + (z/(2 r) + 1)^2))^2 + ((
 q (x/wr - wr/x) (z/(2 r)))/(
 q^2 (x/wr - wr/x)^2 + (z/(2 r) + 1)^2))^2)^(1/2);
A[x_] == 20*Log10[s[x]];

data = Import["C:\\Users\\Farzad\\Desktop\\ESR1.csv"];

nlm = NonlinearModelFit[
data, {A[x], q > 0, wr > 0, 
r > 0}, {{q, 9000}, {wr, 42000000}, {r, 0.1}}, x][
"BestFitParameters"]
Show[Plot[nlm, {x, 41000000, 43000000}, AspectRatio -> Full, 
PlotRange -> {{4.19*10^7, 4.25*10^7}, {0, -55}}], 
ListPlot[data[[All, {1, 2}]], PlotRange -> All, PlotStyle -> Red]]

but for my output I get the following:

output

I'm not sure what the problem is and why I don't get any values for my fit parameters.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Chris K May 16 '18 at 1:06
  • 2
    $\begingroup$ You should replace the == in A[x_] == 20*Log10[s[x]]; with a single = (and follow @BobHanlon 's advice below. $\endgroup$ – JimB May 16 '18 at 2:26
2
$\begingroup$

You have defined nlm as rules for the parameters rather than the model

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};

nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, x]["BestFitParameters"]

(* {a -> 1.50632, b -> 1.42633} *)

Whereas, if you use parentheses to isolate the definition of the model

(nlm = NonlinearModelFit[data, Log[a + b x^2], {a, b}, 
    x])["BestFitParameters"]

(* {a -> 1.50632, b -> 1.42633} *)

Plot[nlm[x], {x, Min[data[[All, 1]]], Max[data[[All, 1]]]},
 Epilog -> {Red, AbsolutePointSize[4], Point[data]}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hey Bob, even when I make that change I get the same output! $\endgroup$ – user171881 May 16 '18 at 2:01
  • $\begingroup$ Did you change the argument to Plot to nlm[x] as shown? $\endgroup$ – Bob Hanlon May 16 '18 at 2:12
  • $\begingroup$ @BobHanlon I think the error occurs because of the use of == in A[x_] == 20*Log10[s[x]];. But I agree about the issue you found with the assignment of nlm. $\endgroup$ – JimB May 16 '18 at 2:21
  • 1
    $\begingroup$ @JimB - since the OP did not provide data, I was not able to run the code and did not notice the improper definitions of both A and s. $\endgroup$ – Bob Hanlon May 16 '18 at 2:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.