Mara 01010011
Mara 01010011

Reputation: 15

How to solve a matlab fit?

So I want to solve a plot fit for several points, but the problem, that I am facing right now is, that my fit object fit_eq is a char but solve needs a sym. I searched everywhere and couldn't find a solution how to fix this. Here is my code, I cut unimportant parts, and some variables are german words, so don't get confused. gesamt is a 60x20 matrix, where every odd column is the same (it's made out of 10 matrices which are an outcome of a meassurement).

anzahlproben = 10;
for i = 1:2:anzahlproben*2 
   probe = gesamt(:,i:i+1); 
   [row c]=find(probe==0); 
   row(1:2,:)=[]; 
   for j=row
      probe(j,:)=[];
   end
   N22_{(i+1)/2} = probe;
end

for i = 1:1:anzahlproben
    x = N22_{i}(1:1:size(N22_{i},1),1);
    y = N22_{i}(1:1:size(N22_{i},1),2);
    ft = fittype('poly9');
    fitobject_{i}=fit(x,y,ft);
end

cvalues = coeffvalues(fitobject_{1});
cnames = coeffnames(fitobject_{1});
fit_eq = formula(fitobject_{1});
for ii=1:1:numel(cvalues)
    cname = cnames{ii};
    cvalue = num2str(cvalues(ii));
    fit_eq = strrep(fit_eq, cname , cvalue);
end
y=1;
syms x
erg = (solve(fit_eq == y,x))

I got the last part from here and gives an equation in a char.

Matlab gives the output:

erg =

Empty sym: 0-by-1

Which can't be right. Any ideas?

Upvotes: 1

Views: 195

Answers (1)

obchardon
obchardon

Reputation: 10792

As mentioned in the first line of the doc:

Support for character vector or string inputs has been removed. Instead, use syms to declare variables and replace inputs such as solve('2*x == 1','x') with solve(2*x == 1,x).

So eqn should be of class sym not a string!

Try:

erg = solve(str2sym(fit_eq) == y,x)

If it still don't work either your equation is wrong or you've not declared the symbolic variables involved in your equation.

Upvotes: 4

Related Questions