Help plotting piecewise function?
Show older comments
I'm new to matlab and was wondering if someone could help me plot the following function as I don't really know where to start....
x is an integer value between 1 to 10
V_{x}N= if x is even, [(3/x)*(3/(x-2))...*(3/4)]*2*N^x
if x is odd, [(5/x)*(5/(x-2))...(5/3)]*pi
N is arbitrary but a value of N=1 is passed into the function in order to plot it
2 Comments
Aye
on 26 Jul 2020
Mario Malic
on 26 Jul 2020
for x = 1 : 1 : 10
if mod(x,2) == 0
y(x) = [(3/x)*(3/(x-2))...*(3/4)]*2*N^x
else
y(x) = [(5/x)*(5/(x-2))...(5/3)]*pi
end
end
Vector y contains your functions from 1 to 10, then you'll have probably have to use for again to plot each on its interval if I understood correctly.
Answers (1)
KSSV
on 26 Jul 2020
YOu can proceed like this:
x = 1:10 ;
y = zeros(size(x)) ;
N = 1 ;
% x is even
idx = 2:2:length(x) ;
y(idx) = [(3./x(idx)).*(3./(x(idx)-2))*(3/4)]*2.*N.^x(idx) ;
% x is odd
idx = 1:2:length(x) ;
y(idx) = [(5./x(idx)).*(5./(x(idx)-2))*(5/3)]*pi ;
plot(x,y)
Categories
Find more on Image Arithmetic 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!