Clear Filters
Clear Filters

Store as array not variable

4 views (last 30 days)
Alex
Alex on 28 Jul 2011
I have the following function:
function f = function42(params2,T0,theta_f)
% f = x^2 %f and x are dummy variables
global a b
nu = params2(1);
phi = params2(2);
wf2 = params2(3);
c1 = b^2*(((exp(a*2*pi)-cosh(a*b*T0))/sinh(a*b*T0))^2-1);
Fitted_Curve = c1*exp(-2*a*theta_f)+nu*((1+0.5*(4*a^2+1))*cos(theta_f+phi)-2*a*sin(theta_f+phi))+b^2-wf2
f = sum(abs(Fitted_Curve));
T0 and theta_f are 1x9 vectors Fitted_Curve comes out in the workspace as a 1x9 vecot also, however c1 does not, it comes out as a variable and therefore i dont think it is using the corresponding T0 with theta_f for each calculation. My question is how do i get c1 to be a vector so that the corresponding c1 is used with the corresponding theta_f in the equation Fitted_curve.
Side note.
anyone know if i am doing the least squares method correctly here in this function.

Accepted Answer

Matt Tearle
Matt Tearle on 28 Jul 2011
The classic trap!
c1 = b^2*(((exp(a*2*pi)-cosh(a*b*T0))/sinh(a*b*T0))^2-1);
The divide there will be interpreted as a matrix divide (ie solving a matrix equation). Sounds like you were wanting elementwise calculation, so change the / into a ./ Similarly, you will also need to change c1*exp(-2*a*theta_f) on the next line to c1.*exp(-2*a*theta_f)
As to the side note: try f = norm(Fitted_Curve) or f = Fitted_Curve*(Fitted_Curve') if you want least squares.
Also, finally: don't use global! Pass a and b in as parameters. If necessary, use anonymous function handles to "wrap" function42.
  2 Comments
Alex
Alex on 28 Jul 2011
Cheers Matt for your help. I have made those changes however it now says that Matrix must be square in the c1 line. the only thing in there is the T0 which is a 9x1 vector
T0 = [1.449 1.442 1.544 1.512 1.511 1.515 1.623 1.637 1.519];
Matt Tearle
Matt Tearle on 28 Jul 2011
Oops, missed the ^2 Guess what, that should be a .^2. Any multiplicative operator (/ \ * ^) will be interpreted in a matrix sense. The elementwise equivalent is made by preceding with a dot (./ .\ .* .^)

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!