Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

what's wrong here? plz help

1 vue (au cours des 30 derniers jours)
B
B le 2 Avr 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaire
Maria Esmeral
Maria Esmeral le 2 Avr 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.

Réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 2 Avr 2015
That means that err is always bigger than 0.01.

Maria Esmeral
Maria Esmeral le 2 Avr 2015
Modifié(e) : Maria Esmeral le 2 Avr 2015
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 le 2 Avr 2015
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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by