You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An error occurred while propagating data type 'double' through...
128 views (last 30 days)
Show older comments
Dear
I'm doing an iterative algorithm in MATLAB/Simulink/Matalb Function. However, when i run the simulation, the errors occurred.(please see the attached picture and code). If possible, please help me. Thank you very much for your help.
function gamma = cg(gamma0,voutx,tao,e1,e2,M)
beta1=gamma0(1);
beta2=gamma0(2);
beta3=gamma0(3);
beta4=gamma0(4);
beta5=gamma0(5);%gamma0 is a vector.
xd=20;%设定位置x
T=0.01;%积分步长
falf=fal(tao,0.5,0.1);
fal1=dfal(e1,0.7,0.1);
fal2=dfal(e2,0.95,0.1);
b0=1;
k=1;
N=100000;%times maximum
epsilon=1e-5;%parameter
gamma=gamma0;
while(k<N)
g=gradient(xd,voutx,fal1,fal2,falf,T,b0,M,gamma);%gradient
beta1=beta1-2*(xd-voutx)*M*(beta4*fal1*T*tao)/(1+(beta4*fal1*T+beta5*fal2)*T*b0);
beta2=beta2-2*(xd-voutx)*M*T*falf*(beta4*fal1*T+beta5*fal2)/(1+(beta4*fal1*T+beta5*fal2)*T*b0);
beta3=beta3-2*(xd-voutx)*M*((beta4*fal1*T+beta5*fal2)*T*T+T/b0)*falf/(1+(beta4*fal1*T+beta5*fal2)*T*b0);
beta4=beta4-2*(xd-voutx)*M*fal1/(1+(beta4*fal1*T+beta5*fal2)*T*b0);
beta5=beta5-2*(xd-voutx)*M*fal2/(1+(beta4*fal1*T+beta5*fal2)*T*b0);%the three parameters that need to be adjusted
gamma=[beta1;beta2;beta3;beta4;beta5];%vector
if(norm(g)<epsilon), break; end
end
19 Comments
Joe Jones
on 17 Jul 2022
Dear
After I ran the model, it only showed the errors in the first Figure, and there was no other error information. My idea is to optimize the parameters of ADRC and I will show you the complete model now.@Walter Roberson
Fangjun Jiang
on 18 Jul 2022
The "complete model" doesn't help to show the error. The error indicates there is a "chart" and points to the place, "ADRC_dingdian.../cgadrc2". Click that link and where does it lead to in the model? Show all block names to help identifying the model blocks.
Joe Jones
on 18 Jul 2022
Dear
Sorry. All block names have been shown in the picture. When uploading the picture, I modified the name of the blocks. I re-simulated after unifying the name, but the same error is still displayed.I click the link and it leads to the MATLAB function 'frcg'. Thank you! @Fangjun Jiang@Walter Roberson
Fangjun Jiang
on 18 Jul 2022
Edited: Fangjun Jiang
on 19 Jul 2022
Open the MATLAB Function block code, click "edit data" to see if there is anything obvious regarding the data types of the parameters and signals.
The code seems to have problems.
- In the while loop, k never increases so it is an infinite loop. You need to add k=k+1
- the usage of gradient() function is puzzling. xd is a constant. Why there are so many input arguments?
Joe Jones
on 19 Jul 2022
Edited: Joe Jones
on 19 Jul 2022
Dear
I modify the code according to your comments.I click "edit data" and change the 'DataType' to 'double'. Then the errors are solved. However, a new error appears:
"Output argument 'gamma' is not assigned on some execution paths."
My 'gamma' is the output of the matlab function, but my output is not in the if-else statement. If possible, please help me. Thank you. @Fangjun Jiang
function gamma=frcg(gamma0,voutx,tao,e1,e2,M)
k=0;
n=length(gamma0);
N = 10000;
epsilon = 1e-5;
xd=20;
T=0.01;
b0=1;
falf=fal(tao,0.5,0.1);
fal1=dfal(e1,0.7,0.1);
fal2=dfal(e2,0.95,0.1);
g0=zeros(5,1);
d0=zeros(5,1);
while(k<N)
g=tidu(xd,voutx,fal1,fal2,falf,T,b0,M,gamma0,tao);
itern=k-(n+1)*floor(k/(n+1));
itern=itern+1;
if(itern==1)
d=-g;
else
beta=(g'*g)/(g0'*g0);
d=-g+beta*d0; gd=g'*d;
if(gd>=0.0)
d=-g;
end
end
if(norm(g)<epsilon), break; end
alpha=-(g'*d)/(d'*d);
gamma0=gamma0+alpha*d;
g0=g; d0=d;
k=k+1;
gamma=gamma0;
end
Fangjun Jiang
on 19 Jul 2022
There is one possibility. If "if(norm(g)<epsilon), break; end" is satisfied during the first iteration, then gamma is never assigned.
Add "gamma=gamma0" as the first line of the function as the default return value to resolve this error.
Joe Jones
on 20 Jul 2022
Dear
I have added "gamma=gamma0" as the first line of the function, however, the function does't work. The error is the same as former. The change has been mentioned with '%' in the attached code. If possible, please help me. Thank you!@Fangjun Jiang
function gamma=frcg(gamma0,voutx,tao,e1,e2,M)
k=0;
n=length(gamma0);
N = 10000;
epsilon = 1e-5;
xd=20;
T=0.01;
b0=1;
falf=fal(tao,0.5,0.1);
fal1=dfal(e1,0.7,0.1);
fal2=dfal(e2,0.95,0.1);
g0=zeros(5,1);
d0=zeros(5,1);
g=g0;
while (k<N)
gamma=gamma0;%I have added this statement to the first line of 'while'.
g=tidu(xd,voutx,fal1,fal2,falf,T,b0,M,gamma0,tao);
itern=k-(n+1)*floor(k/(n+1));
itern=itern+1;
if(itern==1)
d=-g;
else
beta=(g'*g)/(g0'*g0);
d=-g+beta*d0; gd=g'*d;
if(gd>=0.0)
d=-g;
end
end
if norm(g)>epsilon, break; end
alpha=-(g'*d)/(d'*d);
gamma0=gamma0+alpha*d;
g0=g; d0=d;
k=k+1;
%gamma=gamma0; This statement has been moved to the firat line.
end
Fangjun Jiang
on 20 Jul 2022
Not like that!
Use your previous version of code
Add "gamma=gamma0" as the first line of the function, which means, right below the "function gamma=frcg(gamma0,voutx,tao,e1,e2,M)" line.
Joe Jones
on 21 Jul 2022
Dear
I am sorry for misunderstanding your comments.
I modified the code according to your comments. The previous error was indeed resolved, but there is a new one.(please see the attached figure)
I have an ideal: Is it because it may exponentiate negative numbers that it produces complex value?(e.g. a^b: a<0, b is fraction)
I have put the 'tidu' functionn, 'fal' function and 'dfal' function behind main function. If possible, please help me. Thank you ! @Fangjun Jiang@Walter Roberson
function gamma=frcg(gamma0,voutx,tao,e1,e2,M)
gamma=gamma0;%I have added this statement to the first line.
k=0;
n=length(gamma0);
N = 10000;
epsilon = 1e-5;
xd=20;
T=0.01;
b0=1;
falf=fal(tao,0.5,0.1);
fal1=dfal(e1,0.7,0.1);
fal2=dfal(e2,0.95,0.1);
g0=zeros(5,1);
d0=zeros(5,1);
while (k<N)
g=tidu(xd,voutx,fal1,fal2,falf,T,b0,M,gamma0,tao);
itern=k-(n+1)*floor(k/(n+1));
itern=itern+1;
if(itern==1)
d=-g;
else
beta=(g'*g)/(g0'*g0);
d=-g+beta*d0; gd=g'*d;
if(gd>=0.0)
d=-g;
end
end
if(norm(g)<epsilon), break; end
alpha=-(g'*d)/(d'*d);
gamma0=gamma0+alpha*d;
g0=g; d0=d;
k=k+1;
gamma=gamma0;
end
%tidu function
function g=tidu(xd,voutx,fal1,fal2,falf,T,b0,M,gammak,tao)
g1k=(gammak(4)*fal1*T*tao)/(1+(gammak(4)*fal1*T+gammak(5)*fal2)*T*b0);
g2k=T*falf*(gammak(4)*fal1*T+gammak(5)*fal2)/(1+(gammak(4)*fal1*T+gammak(5)*fal2)*T*b0);
g3k=((gammak(4)*fal1*T+gammak(5)*fal2)*T*T+T/b0)*falf/(1+(gammak(4)*fal1*T+gammak(5)*fal2)*T*b0);
g4k=fal1/(1+(gammak(4)*fal1*T+gammak(5)*fal2)*T*b0);
g5k=fal2/(1+(gammak(4)*fal1*T+gammak(5)*fal2)*T*b0);
g = -2*(xd-voutx)*M*[g1k;g2k;g3k;g4k;g5k];
%dfal functionn
function y=dfal(x,alpha,delta)
if x>delta
y=alpha*x^(alpha-1);
else if x<-delta
y=(-1)^(alpha+1)*alpha*x^(alpha+1);
else
y=delta^(alpha-1);
end
end
end
%fal function
function y=fal(x,alpha,delta)
if abs(x)>delta
y=abs(x)^alpha*sign(x);
else
y=x/(delta^(1-alpha));
end
end
Walter Roberson
on 21 Jul 2022
Yes, negative to a fraction needs complex numbers.
Do complex numbers make sense for the physics involved?
Joe Jones
on 21 Jul 2022
No. As far as I know, the complex numbers doesn't make sense for the research I've done. May I ask how to solve this problem?Thank you ! @Walter Roberson
Fangjun Jiang
on 21 Jul 2022
Check the dfal() function. I think the output y is supposed to be symmetric to the input x. Your function is NOT.
Check the literature to make sure it is implemented correctly. Plot out dfal() vs. x to verify.
Joe Jones
on 22 Jul 2022
Edited: Joe Jones
on 22 Jul 2022
Dear
I modified the code according to your comments. The previous error was indeed resolved, but there is a new one.(please see the attached figure 1)
After I click 'fix' in figure 1 and run it again, the same error is still reported, but it seems that the error type has swapped, but there is still an error (please see the red box in the error figure 2).
I click the blue link and find that it points to the block 'derivative'.(please see figure 3)
May I ask what is the reasonfor this? If possible, please help me. Thank you! @Walter Roberson@Fangjun Jiang
Fangjun Jiang
on 22 Jul 2022
- The direct derivative (du/dt) operation usually would cause problem in digital simulation. Use an approximation of derivative if it is absolutely needed. In your model, two derivatives are multiplied but I didn't see where it is used. If it is for observation only, then delete or comment out all of them.
- In the solver settings, reduce the step size or reduce the relative tolerance, you might be able to get the simulation running without error.
- If there is a true algebraic loop, you need to find it and resolve it.
- ADRC is known to have digital simulation stability issues due to its non-linearity. Check the literature to see if there is recommendation on the settings for Simulink simulation.
Joe Jones
on 23 Jul 2022
Edited: Joe Jones
on 23 Jul 2022
Dear
Thank you for your comments!
What I want to do is the derivative of the system output x with respect to the output of the ADRC, which I then convert to the product of the two derivatives with respect to time, as shown in Figure 1.
The product of the two derivatives is used in the subsystem 'cg1', The content of the subsystem can be seen in figure 2.
I will modify and check my model according to your comments. If I still have questions, I will come back to you. I hope that you can answer me by then. Thank you!
Best Regards!
Joe Jones
on 25 Jul 2022
Edited: Joe Jones
on 25 Jul 2022
Dear
I am so sorry to bother you again!
Over the past days, I checked the model and code according to your comments. The idea of the derivatives can be seen in my previous response on 23rd July 2022. I have turned the relative tolerance down to 1e-7, but it still reported same errors.
Now, I'm still doing my best to modify my model. At the same time, I also sent you the model, and I hope you can help me take a look.
I have put my file in the attachment 'test_model.zip' and you can download it to check. When running, you should run the m-files first. My MATLAB version is 2018b. If possible, please help me. Thank you for your help!@Fangjun Jiang@Walter Roberson
Fangjun Jiang
on 25 Jul 2022
This is probably where the help from this community ends. The simulation of the Simulink model runs but it is extremely slow and it stopped due to instability of the simulation. You need to consult the expert in your field to resolve the problem. Most likely the parameters of the ADRC is too aggressive. Avoid directly derivative.
Joe Jones
on 26 Jul 2022
Dear
Thank you for your time and effort you have put into my problems. Your help is of great significance for me to solve the problem. I will continue to do my best to resolve these errors. Thank you again!@Fangjun Jiang
Wish you every success!
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)