# I want to plot Linear Equations but dont know how to do it on matlab

1 view (last 30 days)
Thomas Portsmouth on 22 Jun 2020
Answered: dpb on 22 Jun 2020
I am fairly new to Matlab and I need to derive 2 linear equations for a) wind speed is between 13km/h - 50km/h; b) wind speed is between 50km/h - 100km/h.
source_directory = 'C:\Users\thoma\OneDrive\Desktop\Project 1';
source_file = dir(fullfile(source_directory, '*.csv'));
file_name = fullfile(source_directory,source_file(i).name);
Wind_Month = file_data(:, 7);
Wind_Year = [Wind_Year; Wind_Month];
Wind_Year = [];
Power1 = [];
Power2 = [];
Day1 = [];
Day2 = [];
for i = 1: length(Wind_Year)
if 13 - 50
Power = m * x + c;
Power1 =[Power1; Power];
Day1 = [Day1; i];
elseif 50 - 100
Power = m * x + c;
Power2 = [Power2; Power];
Day2 = [Day2; i];
end
end
How do I calculate Y (Power) in the linear equations?
Also, how do I get the 13 -50 and 50 - 100 to something like: >=13 <50 and >=50 <100?

dpb on 22 Jun 2020
Since you're still new, let me make some suggestions on coding --
First, use newer features -- if you'll read the doc for xlsread (unless you're using a very old version) you'll see it has been deprecated in favor of alternatives -- for this purpose I'd suggest using a MATLAB table instead of array and so readtable...then it's also easy to do the grouping in the same table...
source_directory = 'C:\Users\thoma\OneDrive\Desktop\Project 1'; % this is good to put into variable so can change
% Q? here -- Why not read the .xlsx workbooks themselves instead of making a separate .csv file???
d=dir(fullfile(source_directory, '*.xlsx')); % use a generic variable for a returned dir() struct; it's not a file, exactly
% Now process the files sequentially...
for i=1:numel(d)
file_name = fullfile(source_directory,d(i).name); % get file in turn from dir struct
tData=readtab;e(file_name); % and pull the data into a table
% Now I'm going to presume the table has headers -- if not, you can create variable names to match
%Wind_Month = file_data(:, 7);
%Wind_Year = [Wind_Year; Wind_Month]; % doesn't seem to make sense to catenate these two vertically???
% Make a new grouping variable based on desired ranges of windspeed
edges=[0, 13, 15, inf]; % set bin edges
tData.WindGrp=Y = discretize(tData.WindSpeed,edges, 'categorical', ...
{'Calm', '13-50', '50-100', 'Gale'}); % create table variable by category
Power=rowfun(@(x,y) polyfit(x,y,1),tData,'GroupingVariables','WindGrp', ...
'OutputVariableNames','PowerPoly1','InputVariables',{'WindSpeed','Power'});
end
Above will give you a fit for each group, not just the two; that's fixable by either only defining the two groups or just using the group categories explicitly. You'll also need to save the result as cell array inside the loop; I just overwrite the output variable each pass above.
I made up a small sample dataset here to test --it produced the following output --
>> rowfun(@(x,y) polyfit(x,y,1),tX,'GroupingVariables','G', ...
'OutputVariableNames','PowerPoly1','InputVariables',{'X','P'})
ans =
3×3 table
G GroupCount PowerPoly1
______ __________ ____________________
Calm 2 0.77278 16.191
13-50 10 1.7144 -3.1833
50-100 8 -0.86311 125.91
>>
from the following table--
>> tX
tX =
20×3 table
X G P
__ ______ ______
11 Calm 24.692
50 50-100 95.668
71 50-100 88.859
15 13-50 40.323
50 50-100 49.942
72 50-100 59.38
91 50-100 52.602
34 13-50 28.341
45 13-50 100.77
98 50-100 22.288
78 50-100 89.286
27 13-50 4.3433
84 50-100 36.556
33 13-50 76.195
31 13-50 65.677
1 Calm 16.964
42 13-50 96.901
49 13-50 56.287
34 13-50 26.501
32 13-50 59.137
>>
the above was created from
X=randi(110,20,1);
followed with the discretization and then P was a multiplier plus a noise component on the X value...
Salt to suit...

R2019b

### Community Treasure Hunt

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

Start Hunting!