fprintf a solution with two variables, one from a vector

3 views (last 30 days)
Trying to fprintf two variables for a problem including a variable from a row vector:
% Create a vector of wind velocities using the vector notation V = [start: step: stop] such that your velocity % vector V = [5, 7, 9, 11, 13] m/s. V = [5:2:13];
% Determine the amount of power that each wind turbine can produce for a given wind speed. p = 1.225; % kg/m^3 a1 = 10; % WT #1 (m^2) a2 = 100; % WT #2 (m^2) a3 = 200; % WT #3 (m^2)
% Power = (3/20)*p*A*V^3 % The power output of each size turbine (a1, a2, a3) is measured at each speed according to vector V.
P1 = (.3)*(1/2)*p*a1*(V.^3); fprintf('\nThe power produced by a 10m^2 wind turbine at velocity %1.0f is %.2f',V,P1)
The power produced by a 10m^2 wind turbine at velocity 5 is 7.00 The power produced by a 10m^2 wind turbine at velocity 9 is 11.00 The power produced by a 10m^2 wind turbine at velocity 13 is 229.69 The power produced by a 10m^2 wind turbine at velocity 630 is 1339.54 The power produced by a 10m^2 wind turbine at velocity 2446 is 4036.99
How can I show V(1,1) [5] is equal to 229.69 from the formula? How can I show V(1,2) [7] is equal to 630.26 from the formula? etc, etc.
Thanks!

Answers (1)

Stephen23
Stephen23 on 12 Nov 2018
Edited: Stephen23 on 12 Nov 2018
Solution:
fmt = 'The power produced by a 10m^2 wind turbine at velocity %1.0f is %.2f\n';
fprintf(fmt,[V;P1])
prints:
The power produced by a 10m^2 wind turbine at velocity 5 is 229.69
The power produced by a 10m^2 wind turbine at velocity 7 is 630.26
The power produced by a 10m^2 wind turbine at velocity 9 is 1339.54
The power produced by a 10m^2 wind turbine at velocity 11 is 2445.71
The power produced by a 10m^2 wind turbine at velocity 13 is 4036.99
Explanation: the fprintf operator is very low-level. It does not do anything magic, like matcing up corresponding elements from multiple input arguments. It simply starts from the first one and when it has finished with that it moves on to the second one, etc. With multiple input arguments A1, A2, etc., it basically does this:
fprintf('...',[A1(:);A2(:);...])
And you can see that this simply gives the data from A1 first (in columnwise order), then the data from A2, etc.. To get the sequence that you want you need to concatenate the data into one array first, and ensure that the data is in the correct order when it is accessed columnwise. So what I showed you is equivalent to this:
A = [V;P1]
fprintf('...',A(:))
which, when you look at the elements of A, gives the data in the required order for your format string.

Categories

Find more on Wind Power in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!