Making a new function with code
1 view (last 30 days)
Show older comments
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 Comment
Stephen23
on 10 Dec 2021
Edited: Stephen23
on 10 Dec 2021
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
Accepted Answer
Voss
on 9 Dec 2021
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
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!