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