what's wrong here? plz help

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 Kommentar

Maria Esmeral
Maria Esmeral am 2 Apr. 2015
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.

Antworten (3)

Azzi Abdelmalek
Azzi Abdelmalek am 2 Apr. 2015

0 Stimmen

That means that err is always bigger than 0.01.
Maria Esmeral
Maria Esmeral am 2 Apr. 2015
Bearbeitet: Maria Esmeral am 2 Apr. 2015

0 Stimmen

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 am 2 Apr. 2015

0 Stimmen

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.

Diese Frage ist geschlossen.

Tags

Gefragt:

B
B
am 2 Apr. 2015

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by