At the moment I am trying to do the following. I want to solve the problem with fsolve documentation. It shoud be somehow possible.
First I define my functions.
function F = nonlinmodel( x, C_0, R_m, L_m, C_m, tan_d, w, Re_Y, Im_Y)
% C_0, R_m, L_m, C_m, tan_d are the parameters I have to fit.
%
% x(1) to x(5) are there relativ changes
%
% w is the step of the x-Axis
%
% Re_Y and Im_Y are the data sets.
%
% F(1) and F(2) are almost an array of 0. As in the documentation
% They are not exactly 0 due to the error
% in the inital condition
% Real part
% Function out of theory minus measured data
F(1) = x(2)*R_m./((x(2)*R_m).^2 + (w*x(3)*L_m - 1./(w*x(4)*C_m)).^2) + w*x(5)*tan_d*x(1)*C_0 - Re_Y;
% Imaginary part
% Function out of theory minus measured data
F(2) = (-w*x(3)*L_m + 1./(w*x(4)*C_m))./((x(1)*R_m).^2 + (w*x(3)*L_m - 1./(w*x(4)*C_m)).^2) + w*x(1)*C_0 - Im_Y;
end
Afterwards I optimize my inital conditions. At least I try. ^^
clear all
close all
%%Load data
Aktor = importdata(['Aktor1.w_l']);
Aktor_freq = Aktor(:,1);
Aktor_ampl = Aktor(:,2);
Aktor_phase = Aktor(:,3);
Re_Y = Aktor_ampl.*cos(deg2rad(Aktor_phase));
Im_Y = Aktor_ampl.*sin(deg2rad(Aktor_phase));
w = 2*pi*Aktor_freq;
%%Define inital values
C_0 = 3.948675967565607e-08;
R_m = 7.120051155668139e+03;
L_m = 25.35529039370214e+00;
C_m = 6.242970891780357e-10;
tan_d = 0.054859859155354e-00;
%%relativ change of the inital values
x1 = [1,1,1,1,1];
%%Start fit routine
f = @(x)nonlinmodel(x, C_0, R_m, L_m, C_m, tan_d, w, Re_Y, Im_Y);
optimal_x = fsolve(f,x1);
After that I am getting an error message.
Subscripted assignment dimension mismatch.
Error in nonlinmodel (line 16)
F(1) = x(2)*R_m./((x(2)*R_m).^2 + (w*x(3)*L_m - 1./(w*x(4)*C_m)).^2) +
w*x(5)*tan_d*x(1)*C_0 - Re_Y;
Error in Fitting>@(x)nonlinmodel(x,C_0,R_m,L_m,C_m,tan_d,w,Re_Y,Im_Y)
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Error in Fitting (line 26)
optimal_x = fsolve(f,x1);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
What am I doing wrong? I cannot figure that out. Can you help me? Best regards Henrik