Trying to plot a piecewise function in different colors

6 views (last 30 days)
Hi all, I am trying to create a line plot of an array I have created. The function itself is made up of 3 seperate piecewise functions and i want each portion of the piecewise function to be a different color/look as one another. Here is my supplied codes. Function creation
%%Simple Harmonic Oscillator
% Andrew Lheureux
% Linear Systems Theory
function dydt = FNhw2(t,y)
%
dydt = zeros(2,1); % Creates a column 'state' vector with zeros
%
%Convert the higher order ODE system m x" + k x = F(t)
% into multiple first order equations
%
% y(1) = x(t)
% y(2) = dx(t) = dy(1)
% dy(2) = x" = (F/m)-(kx/m)
%
% k = 640 N/m, m = 40 Kg
%
dydt(1) = y(2);
dydt(2) =(0.125*sin(5*t)-16*y(1))*((t>=0)&(t<4.71)) + (-0.125-16*y(1))*((t>=4.71)&(t<9)) + (-16*y(1))*((t>=9)&(t<15));
dydt = [dydt(1); dydt(2)];
end
and this is where i call and solve my function, create the array, and plot it.
%% Andrew Lheureux % Harmonic oscillator, MATLAB % Linear Systems Theory
clear all;
% Clears all the variables in workspace.
clc;
% Clears the screen for a clean view
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4]);
% set relative tolerance to .0001 and absolute tolerance to .0001 % In order to select a fixed step size for the ode solver, define a % time span vector with a certain step size. For instance for a step size % of 0.01, define tspan = 0:0.01:10 and set it as an input to the ode45 % function.
[T1,A] = ode45(@FNhw2,[0 15],[0 0],options);
% Solves the ODE using ode45, % Calls function FNhw2 % [0 15] is time range % [0 0] are initial conditions % T1 = time vector array, A = solution array
plot(T1,A(:,1))
%%A1=A(1:89);
%%T1=T(1:89);
%%A2=A(90:172);
%%T2=T(90:172);
%%A3=A(172:end);
%%T3=T(172:end);
%
%%plot(T1,A1,'r')
%%hold on
%%Plot(T2,A2,'g')
%%hold off
%
% plots the solution of the desired function
% where the T1 is the time vector and A is the solution matrix
% The (:,1) is suppressing the second column of array A
You can see one of the many attempts i made in getting this to work. I just cant seem to figure it out

Answers (1)

dpb
dpb on 10 Sep 2014
Looks like the basic idea should work...don't say precisely what your problem is, specifically, but consider the trivial example of
y1=rand(10,1); y2=rand(10,1); % some values to plot
plot([1:10].',y1,'*r-',[10:20].',[y1(end); y2],'*g-')
Note the "trick" is that the second data set begins at the same location as the end of the first and duplicates that point by prepending it on the second data set. Otherwise there would be a gap between the two lines if the second went from 11:20 as your selection does.
IOW, where you've taken
A1=A(1:89);
T1=T(1:89);
A2=A(90:172);
T2=T(90:172);
use instead
A1=A(1:89);
T1=T(1:89);
A2=A(89:172);
T2=T(89:172);
...etc., ...
You could automate this by using a set of indices and incrementing them in a loop referencing the subsections of the original array rather than creating new variables. This would save a lot of coding and be far more flexible--all you have to do is have a way to define the breakpoints then.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!