Info
此问题已关闭。 请重新打开它进行编辑或回答。
what's wrong here? plz help
    2 次查看(过去 30 天)
  
       显示 更早的评论
    
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
 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 个)
  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.
0 个评论
  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.
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



