matlab piece wise function
Show older comments
write a matlab code that plots the following piece wise function f(t) = ( 3t^2 +5 t>0)and f(t)=(-e^t + 5t <0 )
4 Comments
Rik
on 3 Jun 2019
Seems like a clear description of your homework. What is your question?
madhan ravi
on 3 Jun 2019
What have you tried so far?
JONATHAN KATHUZA
on 3 Jun 2019
Edited: Rik
on 3 Jun 2019
darova
on 3 Jun 2019
Is there something we can help you? What is the question?
Answers (1)
Rik
on 3 Jun 2019
That is a good idea of solving your problem. There is one thing you're forgetting in your logic: to trim the parts you don't need in the third case. The solution to that is logical indexing, which is a good idea in the first place.
There is some code missing (because I'm not making your homework for you), but these parts should get you where you need to be.
a=-3;b=4;
if a>=b
error('invalid inputs')
end
t=a:0.01:b;
L=___
y=zeros(size(t));
y(~L)= -exp(t(~L))+5;
y(L)=3*(t(L).^2)+5;
But there is an even better solution: you can put your piecewise function in a single anonymous function and let fplot handle your problems. I'm using different conditions and functions as an example here, but you shouldn't have any trouble adapting it to your own problem.
f1=@(t)-exp(t);%function 1
f2=@(t) cos(t*5);%function 2
cond=@(t) sin(t*15)>0;%when this condition is true, set f2 to 0
f=@(t) f1(t).*cond(t) + f2(t).*~cond(t);
fplot(f,[a b])
Categories
Find more on Logical 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!