Generating piecewise function and plotting it

908 次查看(过去 30 天)
Hi! I'm very new for Matlab at the moment. I tried the solve these piecewise equations by vectorized way. But for the condition one, I get error beacause of putting insufficient argument. I need help and waiting your solutions. here is my solution ;
function y=piecewise(x)
%ilk şart first cond
x1=x(x<-1)
y(x<-1)=-1
%ikinci şart second cond
x2=x(-1<=x & x<2);
y(-1<=x & x<2)=x2.^2+x;
%ücüncü şart third cond
x3=x(2<=x & x<4)
y(2<=x & x<4)=-x3+2
%dördüncü şart fourth cond
x4=x(4<=x & x<6)
y(4<=x & x<6)=x4.^3+2*x.^2-3
%besinci şart fifth cond
x5=x(6<=x)
y(6<=x)=2*exp(0.1*x5-0.6)
x=-5:0.1:10
y=piecewise(x)
plot(x,y)

回答(5 个)

Rohit Sinha
Rohit Sinha 2022-4-4
编辑:Rohit Sinha 2022-4-4
You could try this method
syms x %makes x a symbolic variable
f= piecewise(x<-1, -1, x>=-1 & x<2, x^2+x, x>=2 & x<4, -x+2, x>=4 & x<6, x^3+2*x^2-3, x>=6, 2*exp(0.3*(x-6))); %makes a piecewise function for given conditions
fplot(f) %plots the piecewise function

Christopher Creutzig
Your code mixes some x and x2 etc., as in x2.^2+x, which cannot work, as they have different sizes. Try this small modification:
function y=piecewise_fn(x)
x1=x(x<-1);
y(x<-1)=-1;
x2=x(-1<=x & x<2);
y(-1<=x & x<2)=x2.^2+x2;
x3=x(2<=x & x<4);
y(2<=x & x<4)=-x3+2;
x4=x(4<=x & x<6);
y(4<=x & x<6)=x4.^3+2*x4.^2-3;
x5=x(6<=x);
y(6<=x)=2*exp(0.1*x5-0.6);
end

Amjad Green
Amjad Green 2018-3-20
can you write the entire question in english
  7 个评论
john Snori
john Snori 2021-9-1
We can implement piecewise functions by:
  • Treating each function separately and merge and plot them on the same graph
  • If-else statement along with for-loop
  • Switch-case statement
  • Using built-in function of Matlab which returns the piecewise function using a single-line command.

请先登录,再进行评论。


baskar ayyakkannu
baskar ayyakkannu 2022-9-15
function v = RocketVelocity(t)
if t(0<=t && t<=8)
v = 10*t^2-5*t;
elseif 8<=t && t<=16
v = 624-3*t;
elseif 16<=t && t<=26
v = 36*t +12*(t-16)^2;
elseif t>26
v = 2136*exp(-0.1*(t-26));
else
v = 0;
end
end

baskar ayyakkannu
baskar ayyakkannu 2022-9-15
solve the below error
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar
values. Use the ANY or ALL functions to reduce operands to logical scalar values.
Error in RocketVelocity (line 2)
if t(0<=t && t<=8)
code:
function v = RocketVelocity(t)
if t(0<=t && t<=8)
v = 10*t^2-5*t;
elseif 8<=t && t<=16
v = 624-3*t;
elseif 16<=t && t<=26
v = 36*t +12*(t-16)^2;
elseif t>26
v = 2136*exp(-0.1*(t-26));
else
v = 0;
end
end
  1 个评论
Walter Roberson
Walter Roberson 2022-9-15
Your code has more than one problem.
Your code is only expecting scalar t. If t is a vector then using the && operator with it is wrong. && can only be used with scalars. && is the "short-circuit and", which executes the left side and only bothers to execute the right side of the && if the left side is (scalar) non-zero. For example,
if isfield(fp, 'usega') && usega > 2
does not execute the usega > 2 test unless the left side is true.
If you are expecting vector t then you need to change all of the && to & such as
elseif 16<=t & t<=26
Next problem: if t is a vector then 16<=t & t<=26 would be a vector. if you "if" a vector of values then the result is considered true only if all of the values are non-zero, equivalent to
elseif all(16 <= t(:) & t(:)<=26)
It would be uncommon for all elements in your vector to be within the same numeric range, so most of the time you would end up with the else branch that assigns v = 0
Note that your else branch assigns a scalar 0 even if t is non-scalar.
Next problem: if your t is not a square 2D array, then t^2 is an error. t^2 is t*t where the * is "matrix multiplication", also known as "inner product". For the * operator (and so the ^ operator) the number of columns of the left operand must be the same as the number of rows of the right operand; for the ^ operation that means that the matrix must be square (including scalar). If you want to square each element of a matrix individually you would use the .^2 operation instead of the ^2 operation.
Next: you have
if t(0<=t && t<=8)
after fixing that to
if t(0<=t & t<=8)
then you would be testing t against that range, constructing a logical array the same size as t, and you would be using that logical array to index t (because t(something) is a request to index t). So you would extract all of the elements of t that are in range 0 to 8, and you would be applying "if" to that list of elements, which is equivalent to testing whether they are non-zero; and the all() rule still applies. So you have effectively coded
if all( reshape(t(0<=t & t<=8), [], 1) ~= 0)
which is a weird way to test
if all(t(:) ~= 0)
Remember, your code is only going to pass one of the if/elseif/else tests, so you have problems if your t contains a range of values.
I recommend that you read about logical indexing:

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by