Making a new function with code

조회 수: 18 (최근 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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by