Making a new function with code

11 次查看(过去 30 天)
Hi guys,
im trying to make a new function called poweroutput with code that has been used before in different task, im confused on how i would convert this code into a function so it can be used for a differnet task, in the new function i should be able to change the inputs of the variables.
  1 个评论
Stephen23
Stephen23 2021-12-10
编辑:Stephen23 2021-12-10
Original question by Sebastion Sunny retrieved from Google Cache:
Making a new function with code
Hi guys,
im trying to make a new function called poweroutput with code that has been used before in different task, im confused on how i would convert this code into a function so it can be used for a differnet task, in the new function i should be able to change the inputs of the variables Windspeeds,Cd and deltaT:
clear
close all
%vairables defined
Cp = 0.335;
Ct = 0.042;
Vrated = 11.5; %m/s
Vcutin = 3; %m/s
Vcutout = 25; %m/s
D = 171;
k = 6e6;
j = 100e5;
%Create wind,time and rotational speed variables
deltaT = 0.01;
time = 0:deltaT:300;
WindSpeeds = linspace(0,30,length(time));%m/s
%preallocation
rotorTorque = zeros(length(WindSpeeds),1); %Nm
turbinePower = zeros(length(WindSpeeds),1);
generatorTorque = zeros(length(WindSpeeds),1);
omegaRotor = zeros(length(WindSpeeds),1);
%eulers method
for i = 2:length(time)
rotorTorque(i) = windTurbineRotorModel(WindSpeeds(i),Ct,D,Vcutout,Vrated,Vcutin);
omegaRotor(i) = omegaRotor(i-1)+deltaT*(rotorTorque(i)-generatorTorque(i-1)-(Cd*(omegaRotor)))/j;
turbinePower(i) = omegaRotor(i)*rotorTorque(i);
generatorTorque(i) = k * omegaRotor(i).^2;
end
figure;
plot(WindSpeeds,turbinePower/1000)
ylabel('TurbinePower kW')
hold on
yyaxis right
plot(WindSpeeds,generatorTorque/1000)
hold on
plot(WindSpeeds,rotorTorque/1000)
Thank you

请先登录,再进行评论。

采纳的回答

Voss
Voss 2021-12-9
function poweroutput(Windspeeds,Cd,deltaT)
%vairables defined
Cp = 0.335;
Ct = 0.042;
Vrated = 11.5; %m/s
Vcutin = 3; %m/s
Vcutout = 25; %m/s
D = 171;
k = 6e6;
j = 100e5;
% since Windspeeds and time need to be the same size, build the variable
% time from the input arguments Windspeeds and deltaT:
% time starts at 0, has spacing deltaT, and has the same number of elements
% as windSpeeds
% note: maximum time is no longer necessarily 300
time = deltaT*(0:numel(Windspeeds)-1);
%Create wind,time and rotational speed variables
% deltaT = 0.01;
% time = 0:deltaT:300;
% WindSpeeds = linspace(0,30,length(time));%m/s
%preallocation
rotorTorque = zeros(length(WindSpeeds),1); %Nm
turbinePower = zeros(length(WindSpeeds),1);
generatorTorque = zeros(length(WindSpeeds),1);
omegaRotor = zeros(length(WindSpeeds),1);
%eulers method
for i = 2:length(time)
rotorTorque(i) = windTurbineRotorModel(WindSpeeds(i),Ct,D,Vcutout,Vrated,Vcutin);
omegaRotor(i) = omegaRotor(i-1)+deltaT*(rotorTorque(i)-generatorTorque(i-1)-(Cd*(omegaRotor)))/j;
turbinePower(i) = omegaRotor(i)*rotorTorque(i);
generatorTorque(i) = k * omegaRotor(i).^2;
end
figure;
plot(WindSpeeds,turbinePower/1000)
ylabel('TurbinePower kW')
hold on
yyaxis right
plot(WindSpeeds,generatorTorque/1000)
hold on
plot(WindSpeeds,rotorTorque/1000)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by