plotting in 3 for loop's
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Othman Alkandri
el 2 de En. de 2023
Respondida: Voss
el 3 de En. de 2023
I am using 3 for loops.
frist for loop is used to change the speed in V_Wind
the second loop is used to change the angle theta
the third loop is used to change the speed of the vessel in t_vector
I am trying to have three plots of RWind. Note RWind depends in all the for loop parameters.
Therefore, I am trying to study the impact of theta (2nd loop) and Vwind (third loop) on the RWind
The figures of RWind should be as follows
figure(1) @ V_Wind(1)
plots the three thetas "thetas(1)& theta (2) & theta (3)" with respect to the speed profile
figure(2) @V_Wind(2)
............................................................
figure(3) @V_Wind (3)
............................................................
I am not being able to plot the figures, thus my code run non stop. Could you please let me know if there isa better way to do the plotting or if I made any mistake? Also, is it possible to show at which theta and V_Wind the figure is ploting?
Also, I attachted the .CSV files below.
clc
clear all
%Read speed vs time profile created
ST_prof = csvread("final_speed_profile.csv");
time_step = 1; %seconds
t_vector = ST_prof(:,1); %time in seconds
v= ST_prof(:,2);% m/s
plot(t_vector/60,v,'LineWidth',3)
xlabel('time,minutes')
ylabel('Ferry speed, m/s')
grid on
set(gca,'FontSize',14)
xlim([0 70.5])
%t_vector = 1:100;
%v =0.058:0.058:5.8;
%Cx interpolation
%Get the data from csv file
Cx_profile = csvread("Cxfirst.csv");
angle = Cx_profile(:,1); %RPM
Cx = Cx_profile(:,2); %N*m
%start kurve fitting
fCx = fit(angle,Cx,'linearinterp')
plot(fCx,angle,Cx)
xlabel('angle of attack')
ylabel('Cx')
set(gca,'FontSize',14)
%ylim([110 230])
rho_a = 1;
AT =20;
V_Wind=[6 5 4]; % avg speed of wind in NC Cape Hatteras in 2022 (m/s) [9]
%V_wind = 6.5; % [6.5 6.3 4.7 5.9 5.1 4.5 4.6 5.6 6.4 6.1 5.3 5.9 5.3 5.4 8.3 6.8 6.7 6.2];
theta=[45 90 180];
fCx_intial = fCx(0);
for j = 1:length(V_Wind)
j
%V_Wind = V_Wind(j)
for k = 1:length(theta)
fCx_data = fCx(theta(k))
for i = 1:length(t_vector)
CAA_Psi = -fCx_data; %fCx(theta);
CAA0=fCx_intial; %(0); %0.608982
RWind = 0.5*rho_a*CAA_Psi*AT*(V_Wind(j)^2)-0.5*rho_a*CAA0*AT*(v(i)^2); % Added resistance due to wind [N]([8] page 29, eq. 4.2)
RWind= abs(RWind);
hold on
figure(j)
plot(t_vector,RWind,'LineWidth',3)
title('freq vs. time ')
xlabel('time, seconds')
ylabel('fre, Hz')
set(gca,'FontSize',14)
end
hold off
end
end
0 comentarios
Respuesta aceptada
Voss
el 3 de En. de 2023
The reason you don't see any plots coming from this line in the nested 3 for-loops
plot(t_vector,RWind,'LineWidth',3)
is that RWind is a scalar. Plotting a scalar value against a vector creates N lines, where N is the length of the vector (in this case t_vector), and each line has one point. You cannot see a line with only one point, unless it has a data marker.
Example:
plot([1 2 3 4],1,'LineWidth',3) % four lines
plot([1 2 3 4],1,'LineWidth',3,'Marker','o') % four lines, with 'o' marker
That's also the reason it takes the loops so long to finish - you're creating 1501 lines (which is length(t_vector)) each time that plot command is called, and it's called length(V_Wind)*length(theta)*length(t_vector) = 3*3*1501 = 13509 times, for a total of 1501*13509 = 20277009 lines plotted. That's going to take awhile.
Instead, vectorize your RWind calculation (that is, calculate RWind for all 1501 elements at once), and plot it one time. Also, avoid setting the title, xlabel, ylabel, etc., more than once.
See below for the modified code structure:
clc
clear all
%Read speed vs time profile created
ST_prof = csvread('speed_profile.csv');
time_step = 1; %seconds
t_vector = ST_prof(:,1); %time in seconds
v= ST_prof(:,2);% m/s
figure % I create a new figure each time, to avoid replacing what was plotted with subsequent plot calls
plot(t_vector/60,v,'LineWidth',3)
xlabel('time,minutes')
ylabel('Ferry speed, m/s')
grid on
set(gca,'FontSize',14)
xlim([0 70.5])
%t_vector = 1:100;
%v =0.058:0.058:5.8;
%Cx interpolation
%Get the data from csv file
Cx_profile = csvread('CX.csv');
angle = Cx_profile(:,1); %RPM
Cx = Cx_profile(:,2); %N*m
%start kurve fitting
fCx = fit(angle,Cx,'linearinterp')
figure
plot(fCx,angle,Cx)
xlabel('angle of attack')
ylabel('Cx')
set(gca,'FontSize',14)
%ylim([110 230])
rho_a = 1;
AT =20;
V_Wind=[6 5 4]; % avg speed of wind in NC Cape Hatteras in 2022 (m/s) [9]
%V_wind = 6.5; % [6.5 6.3 4.7 5.9 5.1 4.5 4.6 5.6 6.4 6.1 5.3 5.9 5.3 5.4 8.3 6.8 6.7 6.2];
theta=[45 90 180];
fCx_intial = fCx(0);
for j = 1:length(V_Wind)
figure % new figure each time instead of figure(j)
hold on
%V_Wind = V_Wind(j)
for k = 1:length(theta)
fCx_data = fCx(theta(k));
CAA_Psi = -fCx_data; %fCx(theta);
CAA0=fCx_intial; %(0); %0.608982
RWind = abs(0.5*rho_a*CAA_Psi*AT*(V_Wind(j)^2)-0.5*rho_a*CAA0*AT*(v.^2)); % Added resistance due to wind [N]([8] page 29, eq. 4.2)
plot(t_vector,RWind,'LineWidth',3)
end
hold off
title('freq vs. time ')
xlabel('time, seconds')
ylabel('fre, Hz')
set(gca,'FontSize',14)
end
0 comentarios
Más respuestas (1)
Sulaymon Eshkabilov
el 2 de En. de 2023
Two errors in spelling the data file names. And it is advised to use readmatrix() that is more efficient than csvread().
clc; clearvars
%Read speed vs time profile created
% ST_prof = csvread("speed_profile.csv"); % DATA file name must match with the orginal name
ST_prof = readmatrix("speed_profile.csv"); % DATA file name must match with the orginal name
time_step = 1; %seconds
t_vector = ST_prof(:,1); %time in seconds
v= ST_prof(:,2);% m/s
plot(t_vector/60,v,'LineWidth',3)
xlabel('time,minutes')
ylabel('Ferry speed, m/s')
grid on
set(gca,'FontSize',14)
xlim([0 70.5])
%t_vector = 1:100;
%v =0.058:0.058:5.8;
%Cx interpolation
%Get the data from csv file
% Cx_profile = csvread("CX.csv"); % DATA file name must match with the orginal name
Cx_profile = readmatrix("CX.csv"); % DATA file name must match with the orginal name
angle = Cx_profile(:,1); %RPM
Cx = Cx_profile(:,2); %N*m
%start kurve fitting
fCx = fit(angle,Cx,'linearinterp')
plot(fCx,angle,Cx)
xlabel('angle of attack')
ylabel('Cx')
set(gca,'FontSize',14)
%ylim([110 230])
rho_a = 1;
AT =20;
V_Wind=[6 5 4]; % avg speed of wind in NC Cape Hatteras in 2022 (m/s) [9]
%V_wind = 6.5; % [6.5 6.3 4.7 5.9 5.1 4.5 4.6 5.6 6.4 6.1 5.3 5.9 5.3 5.4 8.3 6.8 6.7 6.2];
theta=[45 90 180];
fCx_intial = fCx(0);
for j = 1:length(V_Wind)
j
%V_Wind = V_Wind(j)
for k = 1:length(theta)
fCx_data = fCx(theta(k))
for i = 1:length(t_vector)
CAA_Psi = -fCx_data; %fCx(theta);
CAA0=fCx_intial; %(0); %0.608982
RWind = 0.5*rho_a*CAA_Psi*AT*(V_Wind(j)^2)-0.5*rho_a*CAA0*AT*(v(i)^2); % Added resistance due to wind [N]([8] page 29, eq. 4.2)
RWind= abs(RWind);
hold on
figure(j)
plot(t_vector,RWind,'LineWidth',3)
title('freq vs. time ')
xlabel('time, seconds')
ylabel('fre, Hz')
set(gca,'FontSize',14)
end
hold off
end
end
0 comentarios
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!