Help!! New to matlab - 'Assignment has more non-singleton rhs dimensions' error

Hello, I'm extremely new to Matlab and don't quite know how to rectify this error- 'Assignment has more non-singleton hrs dimensions. I'm trying to use fminsearch to estimate two parameters - amplitude,temperature from the planck's equation.
I'd be grateful if anyone can help me with this. I have attached my code below:
c=2.997*10.^8; % m/s (speed of light)
h=6.6261*10.^-34; % J.s (planck's constant)
k=1.38*10.^-23; % T/K (boltzmann's constant)
xdata=[5.591e-07 5.808e-07 6.017e-07]; %sample points
ydata=[1.099e+13 1.157e+13 1.202e+13]; %sample points
figure(1)
clf
h1=plot(xdata,ydata,'ko','MarkerFaceColor','k');
set(gca,'YLim',[0,10*10^13]);
set(gca,'XLim',[0.3*10^-6,0.7*10^-6]);
xlabel('Wavelength (m)');
ylabel('Energy Density');
z = @(at) at(1).*(( 2*( c.^2 )*h.*pi )./((xdata.^5).*(exp((h.*c)./(xdata.*k.*at(2)))-1))); % at is a vector used in fminsearch
hold on
at = fminsearch(z,[6.747*10.^12;1000]);

Answers (1)

From
doc fminsearch
...
Arguments
fun is the function to be minimized. It accepts an input x and returns a scalar f, ...
From your function definition,
>> z([6.747*10.^12,1000])
ans =
1.0e+18 *
0.3066 0.6630 1.3137
>>
Your function as written returns a 3-vector, not a scalar. You must fix the function definition to return only a single value to minimize. I'll note that the magnitude of the return value is quite large; this may lead to numeric issues later on, but the size of the output vector is your immediate problem.
ADDENDUM
To estimate parameters of the functional form with fminsearch you must compute the residual vector yourself; see <fminsearch>. But Notes there includes "fminsearch is not the preferred choice for solving problems that are sums of squares" recommending instead lsqnonlin at <lsqnonlin>.
fminsearch only wants the one result of the function if minimizing it directly or the total SSE if computing the residual to fit; you've computed all the responses at each of your input points. This is a start towards computing the SS of residuals but you'd need to subtract y values and sum the squares and return that value. Not feasible with an anonymous function; you'd have to write an m-file and pass handle to it.
OTOH, lsqnonlin does expect the vector of responses for all the observations so your functional form is ok to use it excepting again to estimate the coefficients you need to minimize the SSQ of residual. Per it's documentation, it computes the sum of squares directly given the vector of values but again to estimate the coefficients you need to subtract the y.
Hence, to make a long story short, write
z = @(at) at(1)*2*c^2*h.*pi./(xdata.^5.*(exp(h*c./(xdata*k*at(2)))-1))-ydata;
and use it with lsqnonlin instead of fminsearch
BTW, I see two possible issues here
  1. You have only three data points with which to estimate two parameters; that leaves only one degree of freedom. While feasible, solution may not be very good.
  2. Your starting point estimates don't seem very close; I evaluated the function at those values (without subtracting ydata ) and compared to the ydata vector:
>> z([6.747*10.^12,1000])
ans =
1.0e+18 *
0.3066 0.6630 1.3137
>> ydata
ydata =
1.0e+13 *
1.0990 1.1570 1.2020
There's five (5!) orders of magnitude in value difference between the functional values and the observed data. Maybe the solution can be found but it's not a very good starting guess it would seem.

Categories

Asked:

on 5 Aug 2016

Edited:

dpb
on 5 Aug 2016

Community Treasure Hunt

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

Start Hunting!