Help writing a loop...please
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I know this may be dumb but I was not properly taught how to use MATLAB. I am trying to write a loop that solves for a value using one variable (A), but when my input value exceeds a certain value I need it to solve using a different variable (B) EXAMPLE
I have a range of values: W = 0.188:0.001:1.256
when W <= 5.24/Tp (Tp = 11) Sigma = 0.07
but
when W > 5.24/Tp Sigma = 0.09
Here are my two equations I am using that uses my input variables:
Y = exp(-((0.191.*w*Tp-1)./(sqrt(2)*sigma)).^2)
S_w = 155.*((Hs^2)./((Tp^4).*(w.^5))).*exp((-944./((Tp^4).*(w.^4)))).*(3.3.^Y)
I have tried and can only get sigma to equal 0.09. It never uses 0.07. I really need help on this quick and I appreciate all help. Thanks
3 commentaires
Réponses (1)
alex
le 26 Fév 2012
Yes 'Sigma' is evaluated according to the first value in array of'W' that's why you get only one value if 'Sigma' TO FIX IT: use array of sigmas,that has the same size as 'W' and the values in that sigma array have values either 0.07 or 0.09 according the condition on values of W i.e.: if W(1)<=5.24/Tp,then Sigma(1)=0.07 but if W(1)>5.24/Tp,then Sigma(1)=0.09 the same should be done with rest values of array -you can do it in LOOP IMPLEMENTATION -or you can do it in VECTOR MATH IMPLEMENTATION %however,enough with words here is the CODE that does it-in vector %way,:
function ask_from_portal W = 0.188:0.001:1.256; Tp=11; auxilary_array=W<=5.24/Tp;%where the condition is right has value '1' Sigma=ones(size(W));%allocating the ARRAY of sigmas
Sigma(auxilary_array)=0.09;% if W<=5.24/Tp,then Sigma=0.09; Sigma(~auxilary_array)=0.07;%else %W>5.24/Tp % ~ is logical 'not' in matlab bar(Sigma);%can clearly be seen thats Sigma has 0.07 and 0.09 values %Y=exp(-((0.191.*W*Tp-1)./sqrt(2)*sigma)).^2); %-------END CODE--------------
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!