Help writing a loop...please

3 次查看(过去 30 天)
Stephen
Stephen 2012-2-26
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 个评论
Stephen
Stephen 2012-2-26
I have no code written yet other than my basic input variables (w, Tp)...its the first part of the code that needs to be written, but yet have tried and failed
Stephen
Stephen 2012-2-26
here is what i have...but for some reason my Y and S_w values are not changing. Their staying the same
% Inputing Given Variables:
Hs = 12; % Significant wave height (meters)
Tp = 11; % Peak period (seconds)
% Setting a range of frequency with a lower bound and upper bound
w = 0.188:0.000001:1.256;
% Writting a loop to determine which sigma value to use
ii=0;
% For sigma:
for w=0.188:.021363:1.256
ii=1+ii;
if w <= 5.24/Tp,
sigma(ii)=.07;
else
sigma(ii)=.09;
end
end
% JONSWAP EQUATION:
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)';
% Plotting JONSWAP:
plot(w,S_w,'red')
xlabel('w (rad/sec)')
ylabel('...')
title('JONSWAP Prob. 1A')
grid on

请先登录,再进行评论。

回答(1 个)

alex
alex 2012-2-26
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--------------

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by