Help plotting piecewise function?

14 次查看(过去 30 天)
Aye
Aye 2020-7-26
回答: KSSV 2020-7-26
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 个评论
Aye
Aye 2020-7-26
I'm thinking of creating an if else statement with the following structure
if (mod(x,2)==0)
eq1
else
eq2
end
but am having trouble figuring out how to code the equations
Mario Malic
Mario Malic 2020-7-26
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.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2020-7-26
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)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by