Spline and interpolation?

2 ビュー (過去 30 日間)
Rava Sash
Rava Sash 2018 年 4 月 22 日
コメント済み: Rava Sash 2018 年 4 月 22 日
(I'm very new to programming). I want my code to give me the consumption for every speed. So when I for example write consumption(40) in the command window it gives me the consumption for when the speed is 40 km/h.
This is my code so far:
function c = consumption(v)
load roadster.mat;
x = 2:2:200;
y = consumption_Whpkm;
xx=2:200;
pp=spline(x,y,xx);
plot(xx,spline(x,y,xx),'k-',x,y,'ro')
end

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 4 月 22 日

You are not using input variable v and assigning a value to output variable c inside the function body. From the context of your question, the correct function definition is

function c = consumption(v)
  load roadster.mat;
  x = 2:2:200;
  y = consumption_Whpkm;
  xx=2:200;
  pp=spline(x,y);
  plot(xx, ppval(pp, xx),'k-',x,y,'ro');
  c = ppval(pp, v);  % assigning value to output for input v
end
  1 件のコメント
Rava Sash
Rava Sash 2018 年 4 月 22 日
Thank you! I understand my mistake :)

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 4 月 22 日
You need to use v inside your function. Please see attached spline demo.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by