How to use interp1 command?

조회 수: 4 (최근 30일)
Mohammed Alharbi
Mohammed Alharbi 2021년 9월 26일
편집: Mohammed Alharbi 2021년 10월 4일
I have the code to draw each of these 6 figures as follows:
ph1 = 1;
ph2 = 2;
ph3 = 3;
ph4 = 4;
ph5 = 5;
ph6 = 6;
Vi=30;
Vo=0:0.1:30;
d= Vo./Vi ;
m1= d*ph1;
m2= d*ph2;
m3= d*ph3;
m4= d*ph4;
m5= d*ph5;
m6= d*ph6;
floor1= floor(m1);
floor2= floor(m2);
floor3= floor(m3);
floor4= floor(m4);
floor5= floor(m5);
floor6= floor(m6);
Kcm1 = (1-(floor1./(ph1*d))).*(1+floor1-ph1*d)
Kcm2 = (1-(floor2./(ph2*d))).*(1+floor2-ph2*d)
Kcm3 = (1-(floor3./(ph3*d))).*(1+floor3-ph3*d)
Kcm4 = (1-(floor4./(ph4*d))).*(1+floor4-ph4*d)
Kcm5 = (1-(floor5./(ph5*d))).*(1+floor5-ph5*d)
Kcm6 = (1-(floor6./(ph6*d))).*(1+floor6-ph6*d)
figure (1)
hold on
title('for the output current ripple')
xlabel('Duty Cycle')
ylabel('Kcm')
plot(d,Kcm1)
plot(d,Kcm2)
plot(d,Kcm3)
plot(d,Kcm4)
plot(d,Kcm5)
plot(d,Kcm6)
legend('Phase1','Phase2','Phase3','Phase4','Phase5','Phase6');
grid on
hold off
What I want to do is that based on an input value (which is going to be a value on the X-axis), I want the output to choose the figures out of these 6 figures that give the minimum Y-axis value.
For example,
If I put the input value to be 0.5, which is a duty cycle value on the X-axis, then the output should display and recommend (phase-2, phase-4 and figure 6) as they give the minimum value of Y-axis (exactly 0) at this specific input value of 0.5
Another example,
If the input value is 0.25, then the recommend figure to be displayed is (phase-4 the yellow one) as this also gives the minimum value on the Y-axis, and so on.
I was told that I could use the "interp1" command for each curve and pick the minimum but I don't know how to use it.
Please help me out?

채택된 답변

Stephen23
Stephen23 2021년 9월 27일
편집: Stephen23 2021년 9월 27일
Numbering your variables like that is a red-herring that makes this task more complex.
MATLAB is designed to work efficiently with arrays. Using arrays makes your code much simpler:
ph = 1:6;
Vi = 30;
Vo = 0:0.1:30;
d = Vo./Vi;
m = d(:).*ph; % note the orientation!
Kcm = (1-(floor(m)./m)).*(1+floor(m)-m);
figure (1)
title('for the output current ripple')
xlabel('Duty Cycle')
ylabel('Kcm')
plot(d,Kcm)
legend(sprintfc('Phase%d',ph));
grid on
Using arrays also makes your task much simpler:
inp = 0.5;
tmp = interp1(d,Kcm,inp);
idx = find(tmp==min(tmp))
idx = 1×3
2 4 6
inp = 0.75;
tmp = interp1(d,Kcm,inp);
idx = find(tmp==min(tmp))
idx = 4
Note that the orientation of the data in m is significant, because INTERP1 treats each column as its own dataset.
  댓글 수: 12
Mohammed Alharbi
Mohammed Alharbi 2021년 10월 4일
Hi Stephen,
Thanks for your response.
I already did but still waiting for a help and the thought of asking you here. This is the link for the new post:
https://uk.mathworks.com/matlabcentral/answers/1465904-matlab-function-simulink-simulink-does-not-have-enough-information-to-determine-output-sizes-for-th?s_tid=srchtitle
Thanks a lot!
Mohammed Alharbi
Mohammed Alharbi 2021년 10월 4일
편집: Mohammed Alharbi 2021년 10월 4일
Hi Stephen,
I sloved the previous issue by putting
m = d'*ph;
instead of:
m = d(:).*ph;
But now I have another issue with using the "find" command in Simulink function block as it always returns a variable-length vector. Please have a look at this thread if you have time, I am sure you will be the one who can hlep me out with this
Thannks in advance!
Mohammed

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by