How to use vectors inside the fittype function

5 views (last 30 days)
Hello,
I am using the fit function in order to take experimental data and fit it with an expression. The expression that I need to use also uses multiplication of variables which are vectors, within the expression. The problem is that the fittype is a string, which only "knows" the independent variable and the coefficients, as far as I understand. So it does not work, and I dont know how to send these vectors into the expression so fittype can use it. It is best to give a simplified example:
lets say I have only two data points (x1,y1),(x2,y2), and I have another vector, V, which has two elements in it. Mathematically, the expression I want to fit is A1*V./x ( where x is (x1,x2) ), and as a function this will give an answer which is a vector with 2 elements. But the fittype string would be 'A1*V./x' and the symbol V has no meaning since it is not the dependent variable x, and it is not a coefficient.
how can I solve this?
Thank you, Itai.

Answers (2)

Elizabeth Reese
Elizabeth Reese on 4 Dec 2017
You can do this by supplying an anonymous function for the expression in fittype instead of a string.
For example
g = fittype( @(a1,v,x) a1*v./x, 'problem', 'v' );
f = fit(xdata,ydata,g,'problem',V)
You can read more about this option here .

ITAI EPSTEIN
ITAI EPSTEIN on 4 Dec 2017
Thank you Liz. I will try it. As my code is already in string form, is it possible to combine this with a string to get the final fit model?
Thanks, Itai.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!