how to write a loop that finds the best fit value for each given input pair and plot all the resulted points?

조회 수: 2 (최근 30일)
Hello,
I have t and vl observed data that i plotted first and then find the theoritical points and plot them in the same graph.
To construct the model to find the theoritical values i have to write a code according to following steps.
  1. select a starting pair for t1 and vl1. (in this case t=10 and vl = 3.61).
  2. Calculate A
  3. Calculate B
  4. Calculate A-B
  5. Test abs(A-B); if abs(A-B) < 0.001 stop, then go to 8. if not go to 6
  6. increment vl=vl+0.005
  7. goto 2.
  8. increment t=t+5
  9. goto 1
  10. finally plot each pair for A=B (approximately, within given tolerance)
Below is the draft loop I wrote but i am not sure how to actually make it work. Please help me.
for T=10; % tmax = 90;
for VL=3.61
A = atan((vs2.^2).*D2.*(sqrt(1-(VL.^2/vs2.^2)))./(vs1.^2.)*D1.*(sqrt(1-(VL.^2/vs1.^2))));
B = (((2.*pi.*z)./(VL.*T)).*(sqrt((VL.^2/vs1.^2)-1)));
while abs(A-B)<0.001
T=T+5;
A=A
B=B
break
end
while abs(A-B)>=0.001
VL=VL+0.005;
A=A
B=B
end
end
end
  댓글 수: 6
Mathieu NOE
Mathieu NOE 2021년 10월 14일
hello
I have a problem with your equations
see the two examples below . depending if vl>vs1 or vl<vs1 , one output is real and the other is complex , so there is no way to compare them
are you sure that there is no mistake (like missing abs somewhere) in the equations ?
code
clc
clearvars
% T = [10:5:90];
% VL = [3.6;3.7,3.9,4.1,4.2,4.3,4.4,4.4,4.5,4.6,4.65,4.7,4.7,4.75,4.8,4.82,4.85];
% vs1, vs2, d1,d2 and z are just constant values.
vs1=3.6;
vs2=4.7;
d1=2.9;
d2=3.2;
z=40;
% select a starting pair for t1 and vl1. (in this case t=10 and vl = 3.61).
% Calculate A
% Calculate B
% Calculate A-B
% Test abs(A-B); if abs(A-B) < 0.001 stop, then go to 8. if not go to 6
% increment vl=vl+0.005
% goto 2.
% increment t=t+5
% goto 1
% finally plot each pair for A=B (approximately, within given tolerance)
% case 1
t=10
vl = 3.61 % is slightly above vs1=3.6;
A = atan((vs2.^2).*d2.*(sqrt(1-(vl.^2/vs2.^2)))./(vs1.^2.)*d1.*(sqrt(1-(vl.^2/vs1.^2))))
B = (((2.*pi.*z)./(vl.*t)).*(sqrt((vl.^2/vs1.^2)-1)))
% case 2
t=10
vl = 3.51 % is slightly below vs1=3.6;
A = atan((vs2.^2).*d2.*(sqrt(1-(vl.^2/vs2.^2)))./(vs1.^2.)*d1.*(sqrt(1-(vl.^2/vs1.^2))))
B = (((2.*pi.*z)./(vl.*t)).*(sqrt((vl.^2/vs1.^2)-1)))
gives :
case 1 :
t = 10
vl = 3.6100
A = 0.0000 + 0.9856i
B = 0.5193
case 2 :
t = 10
vl = 3.5100
A = 1.1665
B = 0.0000 + 1.5911i
Anitha Limann
Anitha Limann 2021년 10월 14일
Attached is the given question paper. I am supposed to use a value slightly higher than vs1 and lower than observed vL1.
Please see the attachment.

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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 10월 15일
hello Anitha
so yes there was a small bug in your equations
when you do the computation of A, you fliiped the terms
(vs1.^2.)*d1.*(sqrt(1-(VL.^2/vs1.^2)))
must be replaced by
(vs1^2)*d1*(sqrt((vl^2/vs1^2)-1))
that's the reason I got A complex numbers and not reals numbers because the ter under the square root was negative
I am still working on the code ....
  댓글 수: 6

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by