How to apply formula with if statement

4 Ansichten (letzte 30 Tage)
Drew Mullaney
Drew Mullaney am 19 Mär. 2021
Bearbeitet: Drew Mullaney am 19 Mär. 2021
Hello Community,
First time poster so please go easy on me.
I am also very new to matlab. I have just started doing some data fitting and want to do some work using first principles before using the in built data fitting tools.
I wish to fit my data to a log-normal distribution using schmeisers approximation for phi^(-1) and am able to get this done using excel rows to do the mathematics for me. I would rather use a MATLAB if statement to do the mathematics to automate this process in the future and I have had an attempt at this as you can see below
if freqvector < 0.0028
phiminus1 = -(0.2+((1-freqvector).^(0.14)-(1-(1-freqvector)).^0.09)/0.1596)
elseif freqvector > 0.9972
phiminus1 = 0.2 + ((freqvector.^(0.14)-(1-freqvector).^(0.09))/0.1596)
else
phiminus1 = (freqvector.^(0.135) - (1-freqvector).^0.135)/0.1975
end;
frequencyvector is the array i have defined with my F calculations and phiminus1 will be my resulting array to be calculated.
The maths does not agree with my spreadsheet/hand calcs however and will not give the correct values, is someone able to figure out why?
Thank you for taking the time to read!

Akzeptierte Antwort

Jan
Jan am 19 Mär. 2021
Bearbeitet: Jan am 19 Mär. 2021
You did not show with which input you call your function. You mention, that there is a difference, but we cannot guess, which values you get, which you expect and which of them is wrong.
Therefore I can only guess boldly: Then term "freqvector" implies, that the value is a vector. Remember, that the condition of an if command must be a scalar. Therefore Matlab inserts this automatically:
if all(freqvector < 0.0028) & ~isempty(freqvector)
...
end
Is this wanted? I assume, you want to apply this function to the elements of the vector. Then you need either a loop:
phiminus = zeros(size(freqvector)); % Pre-allocation
for k = 1:numel(freqvector)
if freqvector(k) < 0.0028
phiminus1(k) = -(0.2 + ((1-freqvector(k)).^0.14 - (1 - (1-freqvector(k))).^0.09) / 0.1596);
elseif freqvector(k) > 0.9972
phiminus1(k) = 0.2 + (freqvector(k).^0.14 - (1 - freqvector(k)).^0.09) / 0.1596;
else
phiminus1(k) = (freqvector(k).^0.135 - (1-freqvector(k)).^0.09) / 0.1975;
end
end
or you need a logical indexing:
phiminus = zeros(size(freqvector)); % Pre-allocation
m1 = freqvector < 0.0028;
phiminus(m1) = -(0.2 + ((1-freqvector(m1)).^0.14 - (1 - (1 - freqvector(m1))).^0.09) / 0.1596);
m2 = freqvector > 0.9972;
phiminus(m2) = 0.2 + (freqvector(m2).^0.14 - (1 - freqvector(m2)).^0.09) / 0.1596;;
m3 = ~(m1 | m2);
phiminus(m3) = (freqvector(m3).^0.135 - (1 - freqvector(m3)).^0.09) / 0.1975;
Note: Spaces around operators improve the readability. Use parentheses if they are required or improve the clarity of the code. Too many parentheses look confusing.
  2 Kommentare
Drew Mullaney
Drew Mullaney am 19 Mär. 2021
Jan youre a living legend.
Thank you so much. Very informative, easy to read and works perfectly.
Jan
Jan am 19 Mär. 2021
Thanks, Drew. I'm happy, if I can help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by