How to call the function based on the simulink model?
이전 댓글 표시
Hi there!
I have problem with my code - I get error due to multiple causes.
I need three figures- depending on the different values of the Rd and Ud. I want to place my code in the function conected with the simulink model.
What should I modify?
clc
close all
clear all
Rd1=0;
Ud1=0;
[wt1,wU1,wRd1]=parameters(Rd1,Ud1)
Rd2=5*R;
Ud2=0;
[wt2,wU2,wRd2]=parameters((Rd2,Ud2)
Rd3=0;
Ud3=-0.5*U;
[wt3,wU3,wRd3]=parameters((Rd3,Ud3)
function [wt,wU,wRd]=parameters(Rd,Ud)
U=5;
a=1;
b=1;
R=0.5;
L=0.2;
ep=0.1;
tp=-0.1;
tu=0;
T=L/R;
tk=round(tu+8*T);
tpw=tk;
tk=10*tk;
wt=[tp tu tu+10e-12 tpw tpw+10e-12 tk];
wU=[0 0 U U Ud Ud];
wRd=[0 0 0 0 Rd Rd];
sim('zad3cw2_sim');
t=ans.ws(:,1);
In=ans.ws(:,2);
Il=ans.ws(:,3);
Psn=ans.ws(:,4);
Psl=ans.ws(:,5);
En=ans.ws(:,6);
El=ans.ws(:,7);
sn=ans.wsl(:,1);
sl=ans.wsl(:,2);
tzn=t(end)-tpw;
tzl=t(find(sl>0,1,'first'))-tpw;
figure('name','Wyniki symulacji w funkcji czasu','numberTitle','off');
h1=plot(t,Il,'b',t,In,'r');
hold on
h2=plot(t,Psl,'b',t,Psn,'r','Linewidth',1.5);
h3=plot(t,El,'--b',t,En,'--r','Linewidth',1.5);
hold off
legend([h1;h2;h3],'Prąd u.l.','Prąd u.nl.','Strumień u.l.','Strumień u.nl.','Energia u.l.','Energia u.nl.');
xlabel('Czas, s')
ylabel('Prąd, A, Strumień, Wb, Energie, J');
grid
figure('name','Przebiegi prądów wyznaczanie czasów zaniku','numberTitle','off');
h1=plot(t,Il,'b',t,In,'r');
hold on
h2=plot(t,sl,':b',t,sn,':r');
hold off
legend([h1;h2],'Prąd u.l.','Prąd u.nl.','Koniec zaniku u.l.','Koniec zaniku u.nl.');
text(tzn+tpw,0.1*iz,['tzn = ' num2str(tzn,'%0.3f') ' s'],'HorizontalAlignment','left','BackgroundColor',[1 1 1]);
text(tzl+tpw,0.2*iz,['tzl = ' num2str(tzl,'%0.3f') ' s'],'HorizontalAlignment','left','BackgroundColor',[1 1 1]);
xlabel('Czas, s')
ylabel('Prąd, A, ');
grid
end
댓글 수: 5
Paul
2021년 10월 17일
The second and third calls to parameters have unbalanced parentheses. Assuming that's not really the issue you're interested in, what exactly are you trying to do and what errors are you getting?
Aleksandra Pawlak
2021년 10월 17일
Aleksandra Pawlak
2021년 10월 17일
Paul
2021년 10월 19일
What version of Simulink are you using?
Aleksandra Pawlak
2021년 10월 19일
채택된 답변
추가 답변 (2개)
Walter Roberson
2021년 10월 17일
0 개 추천
What should I modify?
You should get rid of the "clear all": it is erasing variables in the base or function workspace that your code is expecting to exist.
You should probably get rid of the "close all" as well, as that might affect scopes or objects drawn by Simulink.
Getting rid of the "clc" would probably be a good idea too.
댓글 수: 4
Aleksandra Pawlak
2021년 10월 17일
Walter Roberson
2021년 10월 19일
편집: Walter Roberson
2021년 10월 19일
Make sure that you do not have any MATLAB Function Blocks that use "clear all" or "assignin('base')" and that none of your simulink object initialization function invoke clear all either.
Aleksandra Pawlak
2021년 10월 19일
Since you're using 2020b, try changing your sim() command to:
out = sim('zad3cw2_sim','SrcWorkspace','current')
This should probably work, but there are some caveats, so I suggest you check the doc page for sim() to see if they apply to your case.
However, SrcWorkspace is not supported for versions later than 2020b. So if you want a solution that works in 2020b and later versions, you can call sim() with a Simulink.SimulationInput object. Take a look at that doc page and then feel free to reply here if you have any questions on how to make that work.
There are other options should neither of these meet your needs. In fact, this question has come up here several times and some searching should quickly find relevant discussions that cover these other options.
카테고리
도움말 센터 및 File Exchange에서 Sources에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!