Why do I get a size mismatch error when trying to fit a surface using an external function?
显示 更早的评论
Hello MathWorks community!
I'm using the Curve Fitting Toolbox to fit a surface using an external function. My function does a series of operations including things like FFTs, so defining an explicit fitting equation is non-trivial.
I've successfuly used an external function like this to fit a curve (x v. y), but I am struggling to make it work when applied to a surface (x&y v. z). Specifically, I get the following error in fittype:
Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data. This custom equation fails to meet that requirement:
I've double-checked the size of my output matrix and it matches the dimensions of x and y. I've also played around with transpose to see if something easy could make it work. To make things simpler, I then tried replacing my external function with one that doesn't have a lot of calculation steps or operations:
function z = TestFit(x, y, n2)
% a meaningless function to test fittype
l_x = length(x); l_y = length(y);
z = zeros(l_x, l_y);
for i = 1:l_x
for j = 1:l_y
z(i, j) = 2.*x(i) + 3.*y(j).*n2
end
end
I get the same error when I use this function instead, even though the dimensions of z match the dimensions of x & y. Furthermore, I noticed that if I remove the loop, the function will work with Curve Fitting Toolbox app and calls of fittype.
I'd prefer to keep loops within my original function, as I'm not sure I can make it work without them. Does anyone have any insight into how to make that work? Any help would be greatly appreciated!
采纳的回答
更多回答(1 个)
So you say
function z = TestFit(x, y, n2)
z = 2*x+3*y*n2;
end
works, but
function z = TestFit(x, y, n2)
% a meaningless function to test fittype
l_x = length(x); l_y = length(y);
z = zeros(l_x, l_y);
for i = 1:l_x
for j = 1:l_y
z(i, j) = 2.*x(i) + 3.*y(j).*n2
end
end
end
doesn't ?
I think you will have to use
l_x1 = size(x,1); l_x2 = size(x,2);
z = zeros(l_x1,l_x2);
for i = 1:l_x1
for j = 1:l_x2
z(i,j) = 2*x(i,j) + 3*y(i,j)*n2
end
end
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!