How do I get the plot for this code?

조회 수: 11 (최근 30일)
Rava Sash
Rava Sash 2018년 5월 15일
답변: Walter Roberson 2018년 5월 16일
I have used the trapetzoid rule. How do I get the plot for this code?
function T = time_to_destination(x,route, n)
load(route);
if x<0||x>distance_km(end)
error("Too big/small")
end
h=x/n;
j=1./(velocity(linspace(0,x,n+1), route));
T=0.5*h*(2*sum(j)-j(1)-j(end))
end
.
function v = velocity(x, route)
load speed_anna.mat;
d = distance_km;
y = speed_kmph;
dd=0:distance_km(end);
pp=spline(d,y);
v = ppval(pp, x);
plot(d,y,'ro');
hold on
h=fplot(@(x)ppval(pp,x),[d(1),d(end)], '- b')
hold off
if (x<0 | x>d(end))
error ('Fel')
end
end

답변 (2개)

Rava Sash
Rava Sash 2018년 5월 16일
I meant trapezoidal rule* Can't anyone help me? Do I need to convert the vectors to a matrix? If so, how do I do that?
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 5월 16일
Your code is pretty odd but it appears to already be plotting, so I do not understand the question.
Sample input would help.
Rava Sash
Rava Sash 2018년 5월 16일
I don't know why I put both the codes in, but I only want to plot the first function. Sorry for the confusion.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 5월 16일
It is difficult to guess what you want. Perhaps
function T = time_to_destination(x,route, n)
load(route);
if x<0||x>distance_km(end)
error("Too big/small")
end
h=x/n;
X = linspace(0,x,n+1);
j=1./(velocity(X, route));
T=0.5*h*(2*sum(j)-j(1)-j(end));
plot(X, j)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by