How to create a function that returns values based on a piecewise function?

3 views (last 30 days)
Hello, I'm having a problem with an exercise featuring f(x), a piecewise function file where I need to create a function with two input arguments (which I will prompt the user for) and output two vectors, f and x.
The vector x should contain values between the start and end at 0.01 increments, and the vector f should contain f(x) for each value of x
Then on a seperate m-file, prompt the user for their domain and plot f(x) based on that domain.
Since this is a homework, I've used a different question on google to get the basic idea and knowledge to solve my own exercise:
Here's what I have so far if helpful or needed:
On m-file: (ignore the velocities, start time and end times and what not, that's just my homework's variables)
clear all; close all; clc;
%Asking for inputs and defining the variables
prompt1 = 'Input the start time';
prompt2 = 'Input the end time';
start_t = input(prompt1);
end_t = input(prompt2);
%Calling the function
[t, v] = VPiecewise(start_t, end_t);
%Plotting the function
plot(t, v)
xlabel('time take with 0.01 smallest division (s)')
ylabel('velocity in ms^-1')
title('v x s')
On function file:
function [t, v] = VPiecewise(start_t, end_t)
t = start_t:0.01:end_t;
...
end
Haven't been able to get much far unfortunately so if anybody would be able to point me towards a right direction, that'd be immensely helpful!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 21 Aug 2016
f=zeros(size(t))
idx1=t<0
f(idx1)=t(idx1).^2
now do the same for 0<=t & t<=3

More Answers (0)

Categories

Find more on Graphics Performance 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!