Clear Filters
Clear Filters

Plot of a function

2 views (last 30 days)
Saptarshi Bhattacharjee
Saptarshi Bhattacharjee on 2 Jan 2012
I have a problem where I need to plot the curve of f(x) vs x.x=[0,4]. I am having problems as to dealing with the multiple outputs of f(x). Is there a way of accessing each element of x as in arrays and then compute the value of f(x) for that x and store it in another array? Finally i can plot the curve with help of values in x and f(x) vectors
  1 Comment
Walter Roberson
Walter Roberson on 2 Jan 2012
Please do not use my email address as a Tag.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Jan 2012
numx = length(x);
fx = zeros(1, numx);
for K = 1:numx
fx(K) = f(x(K));
end
plot(x, fx);
However, keep in mind that
x = [0,4];
constructs x to have exactly two values, 0 and 4. There is no MATLAB construct to express a continuous range of values. You can use the colon notation to express lists of values, such as
x = 0 : 0.1 : 4;
or you can use linspace(), such as
x = linspace(0, 4, 51);
where the 51 is the total number of points including the two endpoints.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!