Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

what's wrong here? plz help

조회 수: 1 (최근 30일)
B
B 2015년 4월 2일
마감: MATLAB Answer Bot 2021년 8월 20일
hl=0.0; % Assigning lower value xl
hu=2.0; % Assigning upper value xu
R=3;
err=2;
v(1)=10;
i=1;
while err>0.01;
hr = (hl+hu)/2; % Calculating root value xr
Vhr=(pi*((hr)^2)*(3*R-hr))/3;
err=abs(Vhr-30);
if err < 0.1 % Breaking while loop when condition is met
break;
end
Vhl= (pi*((hl)^2)*(3*R-hl))/3; % Calculating f(x) with lower value xl
if Vhl*Vhr < 0 % Check the condition
hu=hr ; % Replacing upper value with root value xr
else
hl=hr ; % Replacing lower value with root value xr
end
end
display (hr)
  댓글 수: 1
Maria Esmeral
Maria Esmeral 2015년 4월 2일
Your values never change, are fixed values (hl, hu...)so your err is always bigger than 0.01. Create vector with your parameters so they can vary with a loop or something like that.

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 4월 2일
That means that err is always bigger than 0.01.

Maria Esmeral
Maria Esmeral 2015년 4월 2일
편집: Maria Esmeral 2015년 4월 2일
hl=[0:0.1:100]; % Assigning lower value xl
hu=[0:0.1:100]; % Assigning upper value xu
R=3;
err=2;
v(1)=10;
i=1;
while err>0.01;
for i=1:length(hl)
for j=1:length(hu)
hr = (hl(i)+hu(j))/2; % Calculating root value xr
Vhr=(pi*((hr)^2)*(3*R-hr))/3;
err=abs(Vhr-30);
Vhl= (pi*((hl(i))^2)*(3*R-hl(i)))/3; % Calculating f(x) with lower value xl
if Vhl*Vhr < 0 % Check the condition
hu(j)=hr ; % Replacing upper value with root value xr
else
hl(i)=hr ; % Replacing lower value with root value xr
end
if err < 0.1
break
end
end
if err < 0.1
break
end
end
if err < 0.1 % Breaking while loop when condition is met
break
end
end
display (hr)
Maybe not the best but it works.

Roger Stafford
Roger Stafford 2015년 4월 2일
The trouble is that your initial values for hl and hu both give the same sign, namely positive, to your function to begin with. For your algorithm to work, it is necessary for the upper and lower function signs to always be opposite. Therefore change to something like hl = 1; and hu = 12; initially, which makes the initial signs opposite.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by