Wesser
Wesser

Reputation: 13

translate excel equation to matlab

I need help translating an excel equation to matlab.

I have translated a simple if-statements in excel such as:

Ps(t)=IF(T(t)<Tp,P,0)

into matlab as:

 if Ps(t)=T(t)<Tp;              
 p;
 0;
 end

Is this correct? and if so how do I translate a more complicated excel equation such as:

=IF(F6>273.16,(IF(AND(AB5>0,Y6>0),MIN(Y6*3600/$AE$11,AB5),0)),0)

Upvotes: 0

Views: 80

Answers (2)

Wesser
Wesser

Reputation: 13

=IF(M5>$AD$17,$AD$5+$AD$8,IF(N5>0,$AD$5+(O4-$AD$5)*EXP((IF(F5>273.16,$AD$11,$AD$14))*0.04167),0))

How would this excel equation be translated to matlab? I am very unclear of how to handle the final nested If statement.

Upvotes: 0

R. Schifini
R. Schifini

Reputation: 9313

If you have Ps(t)=IF(T(t)<Tp,P,0) in Excel then in Matlab you could do:

Ps = zeros(1, length(T));
Ps(T<Tp) = p;

where T is a vector. This reads, define Ps as an array of zeros having the same length as T. For those indexes of T where T(i)<Tp set those in Ps to p. This is logical indexing.

The other expression:

=IF(F6>273.16,(IF(AND(AB5>0,Y6>0),MIN(Y6*3600/$AE$11,AB5),0)),0)

has to be analyzed in terms of how this equation operates in Excel. Is this formula copied along the rows of the same column? From which cell is this formula?

Let's say this was taken from cell J6, then iterating over row i you get:

AE11 = 100; %this is the constant from cell AE11

for i= 2:length(F)
    if F(i) > 273.16
      if  AB(i) > 0 & Y(i) >0;
        J(i) = min( Y(i)*3600/ AE11 ,AB(i-1));
      else
        J(i) = 0;
      end
    else
      J(i) = 0;
    end
end

This could be improved with a vectorized version, even though there seems to be a reference to a different row (AE5, when everything is about row 6).

Upvotes: 1

Related Questions