How to build up linear functions in MATLAB and plot the?

40 views (last 30 days)
Hi, I am new in MATLAB and I want to build a code for these linear functions and plot them to use it in my research paper.
The general shape of the linear fuctions are attached.
The idea from these functions that if I have values for example from (0 to 0.04999) in x axis and i substitute them in the first linear function, I want the output to be from (0-0.25) ...
in the second linear function the input from (0.05 to 0.24999) so the output that I want to be are (0.3 - 0.55).
in the third linear function the input from (0.25 to 0.6) so the output that I want to be are (0.6 - 1).
I dont intrest in exact value of the slope at this stage, I just want general functions to achieve what i want.
Thanks in advance

Answers (3)

Star Strider
Star Strider on 3 Aug 2022
The lilnes are easy enough to plot —
xm = [0 0.04999; 0.05 0.24999; 0.25 0.6];
ym = [0 0.25; 0.3 0.55; 0.6 1];
for k = 1:size(xm,1)
B(:,k) = [xm(k,:); 1 1].' \ ym(k,:).'; % Slopes & Intercepts
hold on
for k = 1:size(xm,1)
plot(xm(k,:), ym(k,:), '-k')
hold off
What do you want to do with them after that?

Sign in to comment.

dpb on 3 Aug 2022
% the lookup table/interpolant function...
y=[0 0.25 0.30 0.55 0.6 1];
x=[0 0.05-eps(0.05) 0.05 0.25-eps(0.25) 0.25 0.6];
% let's use it...
xh=[0:0.01:0.6]; % sample points to evaluate
yh=interp1(x,y,xh); % functional values
plot(xh,yh,'x') % see what it looks like...
results in
plot() by default would draw the line between the breakpoints; hence the markers only here to illustrate...
dpb on 3 Aug 2022
Edited: dpb on 4 Aug 2022
The lookup table aspects of @doc:interp1 can be extremely handy tool for shortening user code such as here -- using it for reverse lookup is also something not to be overlooked.
The only disadvantage here I can see is that one does, indeed, have to use values within each segment only to draw a continuous line segment that will show the discontinuity without a line segment drawn between sections. But, the other solutions also all have to have some other way to handle it that is at least as much code/logic as would be using the segment breakpoints in a loop similarly as to @Star Strider
xq = [0.12 rand(1,9)*0.6]; % Supplied Values For 'x'
yq=fnY(xq); % 'y'
Points = [xq; yq]
hold on
xm=reshape(x,2,[]).'; % convert to S-S's variable from mine
for k = 1:size(xm,1)
plot(xm(k,:), fnY(xm(k,:)), '-', 'DisplayName',sprintf('Segment %d',k))
plot(xq,yq,'rs', 'DisplayName','(xq,yq)')
hold off
which is same plot with slightly different set of randomized xq,yq points...I was just too lazy to go to the extra trouble so just dumped the points out to simulate the lines.

Sign in to comment.

M on 3 Aug 2022
Thanks for all @Star Strider @dpb @Sam Chak.I really got benefits from all of you

Community Treasure Hunt

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

Start Hunting!