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
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.
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)];
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
