MATLAB User Specifying a Fit Function in a Separate File

Click For Summary
SUMMARY

The discussion centers on using a custom fit function in MATLAB to determine constants A, B, and C from a set of data points. The user has defined a function in a separate file named HyperfineShiftFunction and is attempting to use it with the fit function. The error encountered is due to a naming conflict where the variable 'y' is being interpreted as both a coefficient and a dependent variable. The solution involves ensuring that the independent variables are correctly specified without naming conflicts.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of fitting functions and the fittype function in MATLAB
  • Knowledge of how to define and call functions in separate files in MATLAB
  • Basic understanding of data fitting and regression analysis
NEXT STEPS
  • Research how to define and use custom fitting functions in MATLAB
  • Learn about the fittype function and its parameters in MATLAB
  • Explore MATLAB's documentation on handling variable naming conflicts in fitting functions
  • Investigate examples of fitting multiple independent variables in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who are working on fitting models with custom functions and need to resolve variable naming issues in their code.

teroenza
Messages
190
Reaction score
5
I have five data points (x,y,z), with two independent variables (x,y), and have written a function in a separate file z = f(x,y,A,B,C), with three undetermined constants (A,B,C).

How can I use a Matlab fitting program to try and determine the constants? I've written a fit function before where I included a string as the user-spesified fitting function, but am not sure how to tell it to call my function in a separate file.
 
Physics news on Phys.org
I have tried, for example:

Code:
x               = [11/2 11/2 11/2 13/2 13/2]'; % F
y               = [9/2 11/2 13/2 11/2 13/2]'; % Fprime
z               = [0.117 0.114 0.110 0.156 0.151]';

ft = fittype('HyperfineShiftFunction( [x, y], Aprime, Bprime, offset )');
f  = fit( [x, y], z, ft )

Where HyperfineShiftFunction is the separate function file. Matlab complains that "The name y cannot be used for both a coefficient and the dependent variable.". I've tried looking into how to tell it that both x and y are independent, but have not succeeded.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K