Use Matlab to graph piece wise function
4 views (last 30 days)
Show older comments
I am a college student working on a calculus project. I need to graph a piecewise function using Matlab with horizontal axis 1 to 56 and vertical axis 0 to 3000.
W(t)=48+3.64t+0.6363t^2+0.00963t^3, 1≤t≤28
-1004+65.8t, 28≤t≤56
My code so far for my function file is
function y = w(x)
if 1<=x && x<= 28
y = 48+3.64*x+0.6363*x^2+0.00963*x^3;
end
if x>28 && x<=56
y =-1004+65.8*x;
end
And my graph file so far is:
function graphw(xmin,xmax)
n=(xmax-xmin)/1000;
x=[xmin:n:xmax];
for i=1:length(x)
W(i)=w(x(i));
end
plot(x,w(x),'r.')
xlabel('x')
ylabel('y')
0 Comments
Answers (1)
Honglei Chen
on 14 Feb 2012
You need to use .^ when raising a vector to a power. This being said, I would just take advantage of the logical index:
x = 1:0.01:56;
y = zeros(size(x));
idx = find((x>=1) & (x<=28));
y(idx) = 48+3.64*x(idx)+0.6363*x(idx).^2+0.00963*x(idx).^3;
idx = find((x>28) & (x<=56));
y(idx) = -1004+65.8*x(idx);
plot(x,y);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!