FZERO cannot continue because user-supplied function_handle ==> @(M)FindM(​M,N(k),Q,w​n(i)) failed with the error below.

2 Ansichten (letzte 30 Tage)
hello everyone...can you help me with fzero function...
Attempt to execute SCRIPT FindM as a function:
C:
Error in Untitled3 (line 25)
M(i,k) = fzero(@(M)FindM(M,N(k),Q,wn(i)),
in my file save of FindM...
i have this coding to find modulated index....
clear all;
w1=2*pi*50;% fundamental freq of 50Hz
w2=2*pi*2000;% switching freq of 2kHz i.e P=40 therefore be careful with the carrier component
t=0:1e-6:20e-3;
num1=((20e-3)/(1e-6))-(0/(1e-6));%num1=20000;
%M=1;
M=0.01:0.01:2;
num2=(2/0.01)-(0.01/0.01);
for n=1:num2+1
A2=1;% amplitude of sine carrier
A1=A2*M(n);
ModulateSig = A1*sin(w1*t);
CarrierSig = A2*sin(w2*t);
pwm_an = 2*((ModulateSig+CarrierSig)>0)-1;
pwm_bn = 2*((-ModulateSig+CarrierSig)>0)-1;
pwm_ab = pwm_an - pwm_bn;
harm_pwm_an =(2/num1)*abs(fft(pwm_an));
harm_pwm_bn =(2/num1)*abs(fft(pwm_bn));
harm_pwm_ab =(2/num1)*abs(fft(pwm_ab));
amp_pwm_an(n,:)= harm_pwm_an;
amp_pwm_bn(n,:)= harm_pwm_bn; %#ok<*SAGROW>
amp_pwm_ab(n,:)= harm_pwm_ab;
norm_amp_pwm_an(n,:)= (amp_pwm_an(n,:))/2;
norm_amp_pwm_bn(n,:)= (amp_pwm_bn(n,:))/2;
norm_amp_pwm_ab(n,:)= (amp_pwm_ab(n,:))/2;
end
k=M';
ff=polyfit(k,norm_amp_pwm_ab(:,2),8);% fit for fundamental curve
Yf=polyval(ff,M);
%Freq ratio = fm/fs = P = 40 therefore location in the array is P+1 =41
fc=polyfit(k,norm_amp_pwm_an(:,41),8); % fit for carrier curve
Yc=polyval(fc,M);
plot(M,norm_amp_pwm_ab(:,2),M,norm_amp_pwm_ab(:,4),'r',M,norm_amp_pwm_ab(:,6),'g',M,norm_amp_pwm_ab(:,8),'b',M,norm_amp_pwm_an(:,41)),grid
%plot(M,norm_amp_pwm_ab(:,2),M,norm_amp_pwm_an(:,41));
then i try to find wn by using fzero function from above by using this coding:
clear all;
w1=2*pi*50;% fundamental freq of 50Hz
w2=2*pi*2000;% switching freq of 2kHz i.e P=40 therefore be careful with the carrier component
t=0:1e-6:20e-3;
num1=((20e-3)/(1e-6))-(0/(1e-6));%num1=20000;
%M=1;
M=0.01:0.01:2;
num2=(2/0.01)-(0.01/0.01);
for n=1:num2+1
A2=1;% amplitude of sine carrier
A1=A2*M(n);
ModulateSig = A1*sin(w1*t);
CarrierSig = A2*sin(w2*t);
pwm_an = 2*((ModulateSig+CarrierSig)>0)-1;
pwm_bn = 2*((-ModulateSig+CarrierSig)>0)-1;
pwm_ab = pwm_an - pwm_bn;
harm_pwm_an =(2/num1)*abs(fft(pwm_an));
harm_pwm_bn =(2/num1)*abs(fft(pwm_bn));
harm_pwm_ab =(2/num1)*abs(fft(pwm_ab));
amp_pwm_an(n,:)= harm_pwm_an;
amp_pwm_bn(n,:)= harm_pwm_bn; %#ok<*SAGROW>
amp_pwm_ab(n,:)= harm_pwm_ab;
norm_amp_pwm_an(n,:)= (amp_pwm_an(n,:))/2;
norm_amp_pwm_bn(n,:)= (amp_pwm_bn(n,:))/2;
norm_amp_pwm_ab(n,:)= (amp_pwm_ab(n,:))/2;
end
k=M';
ff=polyfit(k,norm_amp_pwm_ab(:,2),8);% fit for fundamental curve
Yf=polyval(ff,M);
%Freq ratio = fm/fs = P = 40 therefore location in the array is P+1 =41
fc=polyfit(k,norm_amp_pwm_an(:,41),8); % fit for carrier curve
Yc=polyval(fc,M);
plot(M,norm_amp_pwm_ab(:,2),M,norm_amp_pwm_ab(:,4),'r',M,norm_amp_pwm_ab(:,6),'g',M,norm_amp_pwm_ab(:,8),'b',M,norm_amp_pwm_an(:,41)),grid;
%plot(M,norm_amp_pwm_ab(:,2),M,norm_amp_pwm_an(:,41));
% Extend the modulation index from 0 to 2.0
% File to calculate M and then plot various
% graph with respect to ws/wo
% Function ff(M) and fc(M) has different coefficient
% order of 8th polynpmial ...
%clear all;
Vrms= 240;
Vs= Vrms*sqrt(2);
Po= 80;
f = 50;
Q = 5;
wn = 0.7:0.001:1.3;
numpoint = (1.3/0.001)-(0.7/0.001);%13
%wn = 1.0582;
%numpoint = 0;
%N = 1.5;
%num2 = 0;
N = 1.2:0.2:2;
num2 = (2/0.2)-(1.2/0.2);
for k = 1:num2+1
for i = 1:numpoint + 1
phi_res = atan (Q * (wn(i) - (1/wn(i)) ));
%____________________________________
%Ls = 313e-6
%tanphi = (3*Vs^2 - sqrt(9*Vs^4-4*(2*Po*2*pi*f*Ls).^2))/(2*2*Po*2*pi*f*Ls);
%cosphi = cos(atan(tanphi))
%____________________________________
M(i,k) = fzero(@(M)FindM_1P(M,N(k),Q,wn(i)),[0 2]); % To obtain M from FindM file using fzero Function
ff(i,k) = 0.4199*M(i,k).^8 - 4.6654*M(i,k).^7 + 19.9384*M(i,k).^6 - 42.4761*M(i,k).^5 + 48.0307*M(i,k).^4 - 28.3127*M(i,k).^3 + 8.1582*M(i,k).^2 - 0.3126*M(i,k) + 0.0273;
fc(i,k) = - 0.0954*M(i,k).^8 + 1.4514*M(i,k).^7 - 7.2819*M(i,k).^6 + 17.1224*M(i,k).^5 - 20.6786*M(i,k).^4 + 12.7520*M(i,k).^3 - 3.9375*M(i,k).^2 + 0.4497*M(i,k) + 0.6235;
%____________________________________
%Ls = 2e-3;
%tanphi = (3*Vs^2 - sqrt(9*Vs^4-4*(2*Po*2*pi*f*Ls).^2))/(2*2*Po*2*pi*f*Ls);
%cosphi = cos(atan(tanphi));
%____________________________________
%____________________________________
%OR specified kr and obtained the Ls
kr = 0.1;
%tanphi (i,k) = ((kr*M(i,k)^2)/4)
cosphi (i,k) = 1/sqrt(1+((kr*M(i,k)^2)/4)^2);
%____________________________________
VoVs(i,k) = pi*M(i,k)*cosphi (i,k)/8; %#ok<*SAGROW>
VdcVs(i,k) = cosphi(i,k)/ff(i,k);
theta_dcdc(i,k) = (acos((pi/(2*N(k)))*(fc(i,k) + (1/2)*ff(i,k)*M(i,k))))*(180/pi);
if wn(i)>1
theta_dcdc(i,k) = -theta_dcdc(i,k);
end
Vo(i,k) = VoVs(i,k)*Vs;
Vdc(i,k) = VdcVs(i,k)*Vs;
%VoVdc(i,k) = Vo(i,k)/Vdc(i,k);
R(i,k) = (Vo(i,k)^2)/Po;
Re(i,k) = 8*R(i,k)/(pi^2);
Ires(i,k) = M(i,k)*Vs*cosphi(i,k)/(2*Re(i,k));
Io (i,k) = Po/Vo(i,k);
%Idev(i,k) = N(k)*Ires(i,k);
%____________________________________
%If specified Ls
%kr(i,k) = 2*pi*f*Ls/Re(i,k);
%___________________________________
end
end
but the error message had me struggle so much... can anyone help me?

Antworten (1)

Stephan
Stephan am 15 Jun. 2020
Declare the code in findM_1P as a function and give it proper input and output arguments, to make it possible for fzero passing the needed values to it and get the result back from the function:
  2 Kommentare
Naufal Abdul Aziz Nuawi
Naufal Abdul Aziz Nuawi am 16 Jun. 2020
thanks for your help sir, but i am little bit confused here as i am not clear about how to give the code as function.....
thanks in advance sir.
Stephan
Stephan am 16 Jun. 2020
Did you read the linked content? There is shown how to write a function

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by