evaluate multiple variables with matlab code, (relooping?)
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hey all, Im trying to evaluate an equation with multiple variables,4, whereby 2 are known and 2 are unknown.
so the equation is f=(c/(2*pi))*sqrt(A/(Vl))
f and A are known V and l are unknown
%
A=0.001019;
c = 343;
V= 0.001:0.0001:0.020;
i=0;
l=0.1:0.001:0.6;
j=0;
fmax=58.7;
f=0;
fwanted = 58.7
(%code----)
for i=1:length(V)
for j=1:length(l)
f = (c/(2*pi))*sqrt((A)/(V(i)*l(j)));
if fwanted ==f
V(i)
l(j)
end
end
end
With the code above i only get one solution. I actually want to code to run like this: take the first element of V, keep it constant and run the function with the columns of l. take the second element of V and run it again and so on and display the solutions at the end. whereby creating multiple combinations of V and l. since my knowlegde is limited on this. can anybody help?
採用された回答
Walter Roberson
2012 年 12 月 20 日
8 件のコメント
kwadwo
2012 年 12 月 20 日
hey walter thanks for the site. But there is nothing on it that seems to solve what i want to do looping wise. do u perhaps have some other advice for me?
Your code has
if fwanted ==f
but fwanted and f are both floating point numbers. They are unlikely to ever exactly equal each other, for reasons described in the link. You should modify your code to test whether they are "close enough" to each other for your purposes.
kwadwo
2012 年 12 月 20 日
Thanks! i solved that problem with
% code
for i=1:length(V)
for j=1:length(l)
f = (c/(2*pi))*sqrt((A)/(V(i)*l(j)));
if abs(fwanted-f) < 0.02
V(i)
l(j)
end
end
but the problem is still that the code runs both vectors i and j by increasing integers of both i and j at the same time, whiles i would rather have it keep the integers of i constant while running the code variating j. I think this will then generate al l true combinations.
That code is not increasing i and j at the same time: it is using one particular i, running through all the j for that one i, then going on to the next value of i, running through all the j for that second i, and so on.
With regards to saving the values: try
Vl = [];
for i=1:length(V)
for j=1:length(l)
f = (c/(2*pi))*sqrt((A)/(V(i)*l(j)));
if abs(fwanted-f) < 0.02
Vl(end+1,:) = [V(i) l(j) i j];
end
end
Later when you are satisfied that the order is acceptable, change the assignment to
Vl(end+1,:) = [V(i) l(j)];
kwadwo
2012 年 12 月 26 日
Thank you very much walter! merry christmas and a happy new year!
That is not an efficient method you have used to find solutions to your equation, Kwadwo. It is important to realize that when there are more unknowns than equations, as in this case, in general there will be infinitely many solutions.
You can see this by solving for the product of V and l as follows:
f = c/(2*pi)*sqrt(A/(V*l))
2*pi*f/c = sqrt(A/(V*l))
(2*pi*f/c)^2 = A/(V*l)
V*l = A*(c/2/pi/f)^2
Now that the product of V and l is determined from known quantities, then V can be set to any desired value out of infinitely many possibilities (except zero) and the corresponding l can be found as the above product divided by such a V value. That uniquely determines the infinite continuum of all possible solutions.
Note: It is a good idea to avoid the use of lowercase letter 'l' as a variable name since it is so easily confused with the numeral '1'.
Hey Roger, I know this method is not efficient, also because of the approximation. what way do u then suggest i use, because i m struggling to find the solutions to an extended equation of the one here above, with more unknown variables, but with a given range. which is presented in the code below. The equation can not be transformed to an equation with all unkowns on one side of the equation. that is why i was trying it out for a less difficult equation. or did I misunderstand u? additionally, matlab seems to get stuck in running the program and does not present any solutions. I have read it is due to the performance of matlab (taking too long)? do u perhaps have a solution for this. the equation in the code is absolutely correct. Walter your input in this would also be highly appriciated.
% code
clc
tic
c = 340.3;
Fn=0.001019;
ln=0.46:0.001:0.8;
Vn=0.00046874:0.00001:0.00081520;
V= 0.003:0.0001:0.008;
h=0.001:0.0001:0.3400;
r=sqrt(Fn/pi);
l01=0.24*r;
l02=0.24*r;
V01=Fn*l01;
lv=0.0002;
i=0;
l=0;
k=0;
u=0;
s=0;
t=0;
fwanted = 58.4;
Vl=[];
for i=1:length(V)
for j=1:length(ln);
for k=1:length(h)
for u=1:length(Vn);
f = (c/(2*pi))*sqrt(Fn/((1.21*(V(i)+Vn(u)))*(((V(i)/(V(i)*Vn(u)+V01)))*... (lv+((ln(j)+l01)*(1+0.5*(((Vn(u)+V01)/V(i))+((ln(j)+l01)/h(k)))+((1/3)*...((Vn(u)+V01)/V(i))*((ln(j)+l01)/h(k))))))+l02)));
if abs(fwanted-f) < 0.01
VlnhVn(end+1,:) = [V(i) ln(j) h(k) Vn(u) i j k u];
end
end
end
end
end
end
the code doesn't give any results at all. It's really frustrating
Roger Stafford
2012 年 12 月 27 日
My comment remains roughly the same as it was before. You now have one equation and four unknowns to vary: Ln, Vn, V, and h. Setting f to 58.4 will still leave an enormous three-dimensional space in which the unknowns can vary.
It is possible to manipulate with your equation so as to isolate h on one side of an equivalent equation with everything else on the other side. Then as you vary Ln, Vn, and V you will get all possible solutions. Beware however - three degrees of freedom take a lot of exploring. For example if each of the three take on a hundred possible values, that gives you a combined total of a million solutions to ponder over. I have a feeling you have better things to devote your time to.
By the way, your attempt to explore that four-dimensional space with four nested for-loops would have to go through over two billions steps. That may account for why you didn't succeed with it. However it might have helped if you had allocated space for VlnhVn before entering the loops.
Roger Stafford
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
