Question about if - else and ploting

2 Ansichten (letzte 30 Tage)
Villanova
Villanova am 19 Aug. 2011
Hi. I am trying to use this to plot.
if w_speed_p >= 1.6 && w_speed_p <= 3.4
beaufort = 2
end
if w_speed_p >= 3.5 && w_speed_p <= 5.4
beaufort = 3
end
subplot(3,1,3)
plot(w_speed_p, beaufort ,'v')
xlabel 'Predicted wind speed in the sea'
ylabel 'Beaufort Number'
set(gca,'ytick',0:10)
Can anyone explain what i am doing wrong please? This is my full code. Thanks in advance.
close all
clear all
clc
% w_speed = wind speed originally in miles per hour (mph)
% d_force = wind drag force in newtowns
z=3; % control parameter
% case 1
w_speed_m = [1; 1.1; 1.5; 2.5; 3; 4; 4.3; 5; 5.9; 6.1]; % in mph
d_force_m = [.05; .06; .09; .1; .11; .15; .21; .25; .3; .35]/(10*z); % in newtowns
% case 2
% w_speed_m = [2.5; 3; 4; 4.3; 5; 5.9; 6.1]; %in mph
% d_force_m = [.1; .11; .15; .21; .25; .3; .35]/(10*z)
w_speed_m = w_speed_m*0.44704 % converts mph to meters per second
speed_force=[w_speed_m d_force_m]; % wind speed and it's corresponding drag force
% USV DATA
l_usv = 0.45; % lenght of usv (metric)
w_usv= .18; % width of usv
h_usv = .09; % height of usv
% NAVY BOAT DATA
l_boat = 12; % length of the navy boat in m
v_boat = 5; % velocity of the boat m/s
% for air at atmospheric pressure and T = 25 degrees centigrade we have:
ro = 1.184; % air density in kg/m^3
mu = 1.849*(10^-5); % air viscosity in kg/m.s
% calculating drag coeff and reynolds number for the model
a = w_usv*h_usv;
c_d = 2*d_force_m./(ro*(w_speed_m.^2)*a); % drag Coeff
re_m = ro*w_speed_m*w_usv/mu; % reynolds number
subplot(3,1,1)
p1 = polyfit(w_speed_m, d_force_m, 1); % returns 2 coefficients fitting r = a_1 * x + a_2
r2 = p1(1).* w_speed_m + p1(2);
plot(w_speed_m, d_force_m, 's', w_speed_m, r2)
xlabel 'Wind Speed (m/s)'
ylabel 'Drag Force (N)'
legend ('Measured data', 'Best fit curve')
subplot(3,1,2)
p2= polyfit(re_m, c_d, 6);
f = polyval(p2,re_m);
% table = [re_m c_d f c_d-f]
plot(re_m, c_d, 's', re_m, f, '-',[0:.01:3.5]*1e4, (.5/z), '--r')
%plot([0:.01:3], 0.5, 'r-')
xlabel 'Reynolds number'
ylabel 'C_D'
legend ('Measured data', 'Best fit curve', 'Final average')
% c_d(6) % the last term of c_d matrix
% prototype reynolds number and drag force
re_p = (ro*v_boat*(12/0.45)*w_usv)/mu;
d_force_p = .5*ro*(v_boat^2)*((12/.45)^2)*(w_usv*h_usv)*c_d(6);
w_average_m = mean(w_speed_m) % wind speed average for the model
%w_average_m = 3;
%d_force_average_m = mean(d_force_m)
% wind speed predictate in the sea
%w_speed_p =(l_usv* w_average_m/l_boat) .*sqrt(d_force_p./d_force_m)
w_speed_p =(l_usv* w_speed_m/l_boat) .*sqrt(d_force_p./d_force_m)
if w_speed_p >= 1.6 && w_speed_p <= 3.4
beaufort = 2
end
if w_speed_p >= 3.5 && w_speed_p <= 5.4
beaufort = 3
end
subplot(3,1,3)
plot(w_speed_p, beaufort ,'v')
xlabel 'Predicted wind speed in the sea'
ylabel 'Beaufort Number'
set(gca,'ytick',0:10)

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 19 Aug. 2011
You need to understand the difference between && and &
&& applies to scalar, true && false
& applies to vector, [true true] & [false false]
[true true] && [false false] causes the error

Weitere Antworten (1)

Kelly Kearney
Kelly Kearney am 19 Aug. 2011
Use logical indexing to get a vector of Beaufort numbers. Right now you're only generating a singe value; when a vector is passed to an if statement, i.e.
x = [0 1 2 3];
if x
% something
end
it is interpreted as
if all(x)
% something
end
What you want is:
beaufort(w_speed_p >= 1.6 & w_speed_p <= 3.4) = 2;
beaufort(w_speed_p > 3.4 & w_speed_p <= 5.4) = 3;
Also note I changed your Beaufort scales, since your values left a gap from 3.4 - 3.5.

Community Treasure Hunt

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

Start Hunting!

Translated by