How to take care of matrix dimensions issue when dividing in a function
Show older comments
I'm trying to fit a function that has a division in it. I'm having problems because when you divide a parameter by a data set, I get matrix dimension problems. This is my simple code:
function [y]=apple(x,par)
a=par(1);
b=par(2);
y=(x+b)/a;
end;
Separate editor:
x = [1 2 3 4 5]; y = (x+3)/2;
%set initial guess
par = [1 1];
lowerlim = [0 0]; upperlim = [10 10];
apar=lsqcurvefit(@apple,a,x,y,lowerlim,upperlim);
I'm trying to solve for the parameters, a & b, by fitting the equation y=(x+b)/a to the data set. I'm supposed to get a=2 and b=3. I'm having problems, because the matix dimension for the parameter is only 1x1 dimension whereas the data set is 1x5. I think this is my problem. How do I go about fixing this?
Answers (1)
Walter Roberson
on 9 Feb 2012
lsqcurvefit() varies the first parameter to the given function, not the second parameter.
function y = apple(x, xdata)
a = x(1); b = x(2);
y = (xdata+b)./ a;
end
and in your other file,
xdata = [1 2 3 4 5]; ydata = (xdata+3) ./ 2;
ab0 = [1 1]; %initial guess
lowerlim = [0 0]; upperlim = [10 10];
ab = lsqcurvefit(@apple, ab0, xdata, ydata, lowerlim, upperlim);
a = ab(1); b = ab(2);
Categories
Find more on Computational Geometry 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!