Can I plot with a function?

1 回表示 (過去 30 日間)
Anastasia Zistatsis
Anastasia Zistatsis 2021 年 3 月 25 日
回答済み: Walter Roberson 2021 年 3 月 25 日
I need to plot the velocity function versus time, but when I do, nothing shows up on the graph. Any ideas?
t = [0,0.52,1.04,1.75,2.37,3.25,3.83];
x = [153,185,208,249,261,271,273];
%plot approximate velocity over [0,3.83]
%part i, create x(t) then find derivative
i = interp1(x,t,0,'spline');
f = @(xx)interp1(t,x,xx,'spline');
velocity = derivf(f,0,0.0001); %88.2979
plot(velocity,[0 3.83],'g')
xlabel('time(s)')
ylabel('position(m)')
hold on
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 25 日
What is the code for your derivf function? It does not appear to be a Mathworks function.
Anastasia Zistatsis
Anastasia Zistatsis 2021 年 3 月 25 日
It's one in my text book:
function [Df]=derivf(f,x,h)
h1=h;
h2=h/2;
Df1=(-f(x+2*h1)+8*f(x+h1)-8*f(x-h1)+f(x-2*h1))/(12*h1);
Df2=(-f(x+2*h2)+8*f(x+h2)-8*f(x-h2)+f(x-2*h2))/(12*h2);
Df=4/3*Df2-1/3*Df1;
end

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 25 日
velocity = derivf(f,0,0.0001); %88.2979
that passes the scalar with value 0 as the x to derivf
Df=4/3*Df2-1/3*Df1;
That calculates an output the same size as x .
You are calling it with x being the scalar 0. So velocity will be a scalar.
plot(velocity,[0 3.83],'g')
velocity is a scalar. When you plot with a scalar x, you get dots at each of the y values... but you do not have markers turned on so you will not even see the dots. You could
plot(velocity,[0 3.83],'*g')
to see the points (velocity,0) and (velocity, 3.83)
I suspect you were thinking that you were asking to plot velocity over a range of x values from 0 to 3.83, but you are not doing so. Automatic plotting like that needs fplot() instead of plot(), and it needs the first parameter to be a function handle or a symbolic expression or a symbolic function.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by