Solve a cubic equation in Octave

Hi. I want to solve aforementioned equation in octave

My aim to find Y2 answer. When I do manual calculation, the answer for Y2 shall have 3 answer,
but when I do in octave I cant get 3 answer when using looping. My code as per below:

Assume shape 1 choosen

%TRY
shape=input ('Please key in Shape Number 1,2,3,4:')
Yo=0.715
sg=9.81
Area=1.859
base=2.6
Q=9.8
height=0.31
yc=1.131

%DETERMINE CAPABILITY OF WEIR BY COMPARING height of weir & Critical Height
  %Calculate hc=Eo-Emin
   Eo=Yo+((Q^2)/(2*sg*(Area^2)))
   Emin=(3/2)*(yc)
   hc=Eo-Emin;
    %Compare hc & height
      if height<hc 
       disp('This Section Case 1')
       Y1=Yo
       Y3=Yo
       E1=Eo
       E3=Eo
       E2=Eo-height
          %Calculate Y2
          if shape==1
          disp('Use Area Rectangular, To find Y2')
            Y2=0.001;1
          while Y2<10
            LHSA=Y2
            LHSB=(Q^2)/(2*sg*((base*(Y2^2))))
            LHS=LHSA+LHSB
            RHS=E2
              if (LHS>=RHS)
              answer_Y2=Y2;
              fprintf('Y2 = %-3.4f\n',answer_Y2)
              break;   
              end
            
              Y2=Y2+0.001; 
          end
          elseif shape==2
          disp('Use Area Triangular, To find Y2')
          elseif shape==3
          disp('Use Area Trapezoidal, To find Y2')
          elseif shape==4
          disp('Use Area Circular, To find Y2')
          else
          disp('Error Shape, To find Y2')
          end  
      elseif height=hc
      disp('height=hc')
      
      elseif height>hc
      disp('height>hc')
    
      else
      disp('Error')
      
      end

From what I understand, the goal is to solve a cubic equation with variable Y2, where the Area is a function of Y2 and the Area could be constructed by different shapes (rectangle, triangle, etc.). To solve the resulting cubic equation, a trial&error (loop) code is used.

First, I would stick to pure math. Formalize for each Area type a single (cubic polynomial) equation (on paper). If you have formalized your problem properly, Octave has for example the roots-function to solve such type of equations.

Hi I try to do as per your comment

My Coding

c = [132.6312 241.57 0 100];
roots (c)

fprintf(‘Roots %-4.4f\n’,roots (c))

However I got different answer from manual calculation
image

ILLYAS_AZMEER
January 4

Hi I try to do as per your comment

My Coding

c = [132.6312 241.57 0 100];

You missed a minus sign
c = [132.6312 -241.57 0 100];

1 Like