problem in plotting data

1 view (last 30 days)
Goodmorning,
I am doing a fit with a complex function: the code seems to work correctely but I cannot plot the data.
Do you have suggestion about what the problem is?
I attach the code I am working on and the data.
close all
clear
clc
load 'f_B100_PEGNA_t30_bisSC.txt'
f_B100_PEGNA_t30_bisSC;
f=f_B100_PEGNA_t30_bisSC(:,1);
yR=f_B100_PEGNA_t30_bisSC(:,6);
yI=f_B100_PEGNA_t30_bisSC(:,7);
Y=yR-1i*yI;
DER=yR.*0.01;
DEI=yI.*0.01;
DE_tot=DER+1i*DEI;
weightR=1./DER;
weightI=1./DEI;
weightT=1./DE_tot;
fit_fun=@(r,f) abs(r(1))+abs(r(2))./(1+(1i*f*2*pi*r(3)).^(abs(1-abs(r(4))))).^(r(5));
fit_funw=@(r) norm(weightR.*real(fit_fun(r,f)-Y)+1j*weightI.*imag(fit_fun(r,f)-Y));
r0=[1,1,1,1,1];
r=fminsearch(fit_funw,r0);
Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 902.572770
r=r(:)
r = 5×1
3.5956 2.7170 -0.0000 1.1588 -2.0959
figure(1)
loglog(f,yR,'o',f,real(fit_fun(r,f)))
figure(2)
loglog(f,yI,'o',f,-imag(fit_funw(r)))
Thank you all!

Accepted Answer

Cris LaPierre
Cris LaPierre on 12 Nov 2021
I changed your post to executable code and ran in here. Two plots were created. Are those the figures you are trying to create? I suspect you expect to see a line in the second plot?
Upon inspection, the result of fit_funw(r) is a single number, 902.5728. It is not a complex number, so the result of imag is 0.
By default, the plot function does not include a markerstyle. It just plots using a solid line. When you data is a single point, you cannot see that line. This is because norm returns a single value for a vector input. Specify a marker style for the second plot.
If you expect fit_funw to return a vector, please check your approach.
  1 Comment
Daniele Sonaglioni
Daniele Sonaglioni on 12 Nov 2021
Thank you @Cris LaPierre but I was experiencing a problem with Matlab and the plots were not visible. Anyway, I have subsequently improved the code with GlobalSearch.
Referring to the question above, the code was right, the only problem was with Matlab but now I have solved everything. If you can, I wuold ask if can delete the question because it is not useful for other users at all.
Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!