## Using curve fitting with solve function

Asked by August Mikaelsson

### August Mikaelsson (view profile)

on 18 Jul 2019
Latest activity Answered by Sai Bhargav Avula

### Sai Bhargav Avula (view profile)

on 2 Aug 2019
Using formula(curvefit) I can get the polynomial function of the curvefit to show in the command window.
I would like to use that polynomial equation togheter with a function handle, that comes from another function file, and pass them to a the solve function to solve for one of the two variables.
I_sc has variables x and y.
I_sc_max only has variable y (the same y as I_sc).
The second problem is that i want to use the solution (one of the solutions) from the solve function as a function handle.
Thanks for any help!
This code is my try at solving the problem but using the == sign doesn't work well with sfit.
Error:
Undefined function 'eq' for input arguments of type 'sfit'.
syms x y;
% Curve fit: poly23.
% formula(curvefit) shows the sfit uses variables x and y.
I_sc = curvefit; % sfit
I_sc_max = I_sc_max_function(); % function handle from other file. Variable y.
eqn = I_sc == I_sc_max; %<--- ERROR
S = solve( eqn, x ); % Gives two solutions
S1= S(1); % Extracting one solution
% Want to use solution as function handle.
func =@(y) S1;

### Release

R2019a

Answer by Sai Bhargav Avula

### Sai Bhargav Avula (view profile)

on 2 Aug 2019

I_sc value is an object of the sfit. The solve function requires a polynomial expression. Using the attributes(coeffvalues,coeffnames) of sfit object the function handle for the polynomial can be obtained.
% Extract coefficient names
names = coeffnames(I_sc);
I_sc_expression = 0;
% Looping to construct the polynomial expression.
for i = 1:numel(names)
name = names{i};
I_sc_expression = I_sc_expression + I_sc.(name)* (x^str2double(name(2)) * y^str2double(name(3)));
end
eqn = I_sc_expression == I_sc_max;
S = solve( eqn, x ); % Gives two solutions
S1= S(1); % Extracting one solution
func =@(y)subs(S1,y);