How to graph equations with different x domains?
7 views (last 30 days)
Show older comments
Muhammad Faaz Usman
on 28 Apr 2021
Commented: Star Strider
on 28 Apr 2021
Suppose I have two equations:
x(t)= -10000e^(-0.05t) + 10000.......(1) where 0<=t<=26
x(t)= -5123.635e^(-0.05t) + 10000.......(2) where 26<=t<=100
How do graph these two equations on the same window ?
I tried to use the hold on hold off function but im failing to do it. Is there any other way to do it? Im a beginner so easy codes please ;)
0 Comments
Accepted Answer
Star Strider
on 28 Apr 2021
First, code it correctly, then use logical indexing —
t = linspace(0, 110);
x1 = @(t) -10000*exp(-0.05*t) + 10000;
x2 = @(t) -5123.635*exp(-0.05*t) + 10000;
v1 = ((t>=0) & (t<=26));
v2 = ((t>=26) & (t<=100));
figure
plot(t(v1), x1(t(v1)))
hold on
plot(t(v2), x2(t(v2)))
hold off
grid
.
4 Comments
Star Strider
on 28 Apr 2021
I would do something like this —
t = linspace(0, 110);
x1 = @(t) -10000*exp(-0.05*t) + 10000;
x2 = @(t) -5123.635*exp(-0.05*t) + 10000;
v1 = ((t>=0) & (t<=26));
v2 = ((t>=26) & (t<=100));
ofst = min(x2(t(v2)))-max(x1(t(v1)))
figure
plot(t(v1), x1(t(v1)))
hold on
plot(t(v2), x2(t(v2))-ofst)
hold off
grid
That is likely as close as it is possible to match them. Even though the both have 26 in common, the small reminaing gap is caused by the resolution of ‘t’.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!