MATLAB Answers

I have the following code. The values of displ in stage1 and stage2 are plotted in displ vs time graph. Thus a single curve is obtained.

1 view (last 30 days)
displ=zeros(100,2)
tim=zeros(100,1)
for i=2:100
v=5;
dt=1*10^-4;
function [displ_f]=stage1(displ_i)
dt=1*10^-4;
v=5;
displ_f=displ_i+v*dt;
endfunction
function [displ_f]=stage2(displ_i)
dt=1*10^-4;
v=5;
displ_f=displ_i+v*dt;
endfunction
if (displ(i-1,1)<0.020 && displ(i-1,2)>=0)
disp("IN stage 1");
[displ(i,1)]=stage1(displ(i-1,1));
displ(i,2)=v;
tim(i,1)=tim(i-1)+dt;
elseif (displ(i-1,1)>=0.020 && displ(i-1,1)<=0.0256 && displ(i-1,2)>=0)
disp("IN stage 2");
[displ(i,1)]=stage2(displ(i-1,1));
displ(i,2)=displ(i-1,2);
tim(i,1)=tim(i-1)+dt;
end
end
disp(" displ vs tim")
plot(tim(:,1),displ(:,1));
How will I get stage1 curve in orange color and stage2 curve in blue color in the same plot to know which stage has which displ values?

  5 Comments

Show 2 older comments
nanren888
nanren888 on 7 Jan 2019
>>help endfunction
endfunction not found.
>>
Happy to help: Please define your strange "endfunction" function.
Is it perhaps not Matlab?
.
There's an old Irish proberb that is about asking an Irish guy how to get to a destination. He simply answers that he wouldn't start from here.
Lakshmikruthiga Ponnusamy
yeah, it is octave. Still, you can help. Matlab and octave both are similar. I didn't get any help from octave forum, so I posted it here. Kindly help.
Walter Roberson
Walter Roberson on 8 Jan 2019
Octave is a product of the Free Software Foundation . The fundamental principle of FSF is that the software should be no cost, and that you can modify it as needed , but that if you want service then you should pay for the service . Therefore if you are not satisfied with the response you got from the Octave distribution list, you should be hiring a consultant . That is the FSF way .
FSF is not a charity to provide free software because software can be hard to afford .FSF is a deliberately disruptive economic model that aims to drive proprietary software out of business, replacing it with no cost for the software and instead charging for work on the software.
FSF says that it is morally and ethically wrong to create software and not give the software away for no charge, that anyone who creates proprietary software is a Bad Person who is Doing Evil.

Sign in to comment.

Accepted Answer

nanren888
nanren888 on 7 Jan 2019
Edited: madhan ravi on 8 Jan 2019
Is this the sort of thing you are looking for?
displ=zeros(100,2)
tim=zeros(100,1)
stageOneMask = false([100,1]);
stageTwoMask = false([100,1]);
for i=2:100
v=5;
dt=1*10^-4;
if (displ(i-1,1)<0.020 && displ(i-1,2)>=0)
disp("IN stage 1");
[displ(i,1)]=stage1(displ(i-1,1));
displ(i,2)=v;
tim(i,1)=tim(i-1)+dt;
stageOneMask(i) = true;
elseif (displ(i-1,1)>=0.020 && displ(i-1,1)<=0.0256 && displ(i-1,2)>=0)
disp("IN stage 2");
[displ(i,1)]=stage2(displ(i-1,1));
displ(i,2)=displ(i-1,2);
tim(i,1)=tim(i-1)+dt;
stageTwoMask(i) = true;
end
disp(" displ vs tim")
plot(tim(:,1),displ(:,1),'k',tim(stageOneMask,1),displ(stageOneMask,1),'Ob',tim(stageTwoMask,1),displ(stageTwoMask,1),'Or');
end
function [displ_f]=stage1(displ_i)
dt=1*10^-4;
v=5;
displ_f=displ_i+v*dt;
end
function [displ_f]=stage2(displ_i)
dt=1*10^-4;
v=5;
displ_f=displ_i+v*dt;
end
Generally, if you know something about the structure of the problem, for example, that it has two distinct stages, maybe your code could represent that.
Sorry, I had to rearrange your code to get it sort of Matlab compliant. (Leaves it a bit of a mess, sorry)
Might want to consider rearranging it to be more in line with normal style guides?
Did I guess correctly what your "stages" were?

  2 Comments

Lakshmikruthiga Ponnusamy
Yeah, your guess was right. I want this type of plot you mentioned. But your answer gives me error as follows. I use octave, sorry for inconvenience.
"error: set: unknown line property Ob"

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!