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

3 次查看(过去 30 天)
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!

采纳的回答

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

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by