Making a new function with code

2 ビュー (過去 30 日間)
Sebastian Sunny
Sebastian Sunny 2021 年 12 月 9 日
編集済み: Stephen23 2021 年 12 月 10 日
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
  1 件のコメント
Sebastian Sunny
Sebastian Sunny 2021 年 12 月 9 日
Ahh thank you that makes more sense

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by