How to save a maxtrix created by for loop

1 回表示 (過去 30 日間)
jaehun chung
jaehun chung 2023 年 3 月 14 日
編集済み: Biraj Khanal 2023 年 3 月 19 日
The code below is what I write to interpolate the function.
To plot the interpolated function, I think fx should be saved in 49 columns.
How can I put fx in a matrix? (1 row and 49 columns)
--------------------------------------------------------
x=linspace(-2,2,13);
xi=linspace(-2,2,49);
f= 3*x.^2 + 8*x;
m = [-1 -0.5 0 0.5 1];
fx=zeros(size(xi));
for i = 1 : length(x) -1
for j = 1 : length(m)
fx = f(i) * (1-m(j)) / 2 + f(i+1) * (1+m(j)) / 2
end
end
plot(xi,fx,'*-')

回答 (1 件)

Biraj Khanal
Biraj Khanal 2023 年 3 月 14 日
Is this what you are trying to do?
x=linspace(-2,2,13);
xi=linspace(-2,2,49);
f= 3*x.^2 + 8*x;
fx = interp1(x,f,xi);
plot(xi,fx)
  2 件のコメント
jaehun chung
jaehun chung 2023 年 3 月 16 日
thanks but it is not what I want.
Im trying to plot the interpolated function without using 'interp1'.
Biraj Khanal
Biraj Khanal 2023 年 3 月 19 日
編集済み: Biraj Khanal 2023 年 3 月 19 日
Your length(x) -1 is 12 and length(m) is 5. hence if you put fx in a matrix by assigning a counter c and increase it in every iteration, you would obtain fx with 60 elements. I am not sure how you are trying to relate it with xi. Can you clarify?

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by