Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

need help with function and lsqcurvefit please

1 visualización (últimos 30 días)
Iris
Iris el 17 de Mayo de 2013
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hi there! :) I'm still learning how to program, and since i dont have any teacher i need some help guys, please.. I've done a few interesting programs with matlab, but still, i run away from functions, because i still can't understand it... In this assignment I need to find a fit for some data. I need a funtion to calculate a sort of things with input parameters being my ydata (time2) and a lot of constants (T, P, e, K, Y, w) that must be found for the curve to fit the real data. The function must be something like this:
function [RV] = RVfun(time2, T, P, e, K, Y, w)
M1 = 2*pi*(time2 - T)/P;
E(:,1) = M1 + e*sin(M1) + (e^2/2)*sin(2*M1);
M(:,1) = E(:,1) - e*sin(E(:,1));
for i = 2:1000
E(:,i) = E(:,i-1) + ((M1 - M(:,i-1))./(1 - e*cos(E(:,i-1))));
M(:,i) = E(:,i) - e*sin(E(:,i));
if max(abs(M(:,i) - M1)) < 10^-13
Mean = M(:,i);
Ee = E(:,i);
break
end
end
ta = 2*atan(sqrt((1 + e)/(1 - e)).*tan(Ee./2)); % True anomaly
RV = Y + K.*(e*cos(w) + cos(w + ta)); % Radial Velocity
end
I think the problem is here, because i have an error that says:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> RVfun at 3
M1 = 2*pi*(time2 - T)/P;
I don't understand this error because if i run this code out of the function, it works perfectly.
Anyway, the code is like this:
% Estimates:
T0 = min(time2);
P0 = 1/f(Per == max(Per));
e0 = 0.4;
K0 = (max(rad_v2) - min(rad_v2))/2;
Y0 = median(rad_v2);
w0 = 0;
x0 = [T0 P0 e0 K0 Y0 w0];
minl = [min(time2) 700 0 0.02 min(rad_v2) 0]; % Lower limit
maxl = [Inf 900 1 0.06 max(rad_v2) 2*pi]; % Upper limit
% Fit data:
options = optimset('Algorithm', 'levenberg-marquardt', 'MaxIter', 1000, 'TolFun', 1e-10, 'TolX', 1e-10);
[x, resnorm] = lsqcurvefit(@RVfun, x0, time2, rad_v2, minl, maxl, options);
Can you see what am I doing wrong?
Thank you! :)

Respuestas (0)

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by