Can I plot with a function?

조회 수: 4 (최근 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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by