How to generate a function from x-y vectors?

81 ビュー (過去 30 日間)
Ajay Goyal
Ajay Goyal 2017 年 2 月 6 日
コメント済み: Ghenam Dahmane 2022 年 3 月 4 日
Hello MATLAB users. I got two vectors (say x and y). Is there any way to create a function i.e. y=f(x). I do not want the expression. I just want to create the function in matlab for further calculations. For ease: x=[1 5 0 32 8]; y=[10 1 654 32 100];
  2 件のコメント
the cyclist
the cyclist 2017 年 2 月 6 日
Your question is not clear. What does "use these vectors in function form" mean?
Do you mean you want to find a function f(x) that will (approximately?) fit the x-y data? If so, what type of function? A polynomial?
Ajay Goyal
Ajay Goyal 2017 年 2 月 6 日
編集済み: Ajay Goyal 2017 年 2 月 6 日
If I do not get expression; its ok. I just want to create a funtion from these vectors so that further calculations like Fourier series etc can be done

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

採用された回答

Nicolas Schmit
Nicolas Schmit 2017 年 9 月 4 日
You question is not very clear, so I will try to guess what you are trying to do.
If you want to define a purely symbolic function f(x), use the following syntax.
syms f(x)
If you want to create a function from data points x and y, use the griddedInterpolant function to create a interpolation of x and y.
x=[1 5 0 32 8];
y=[10 1 654 32 100];
[~, index] = sort(x);
F = griddedInterpolant(x(index), y(index));
  2 件のコメント
Ajay Goyal
Ajay Goyal 2017 年 9 月 4 日
Thank You, Nicolas Schmit
Ghenam Dahmane
Ghenam Dahmane 2022 年 3 月 4 日
Thank you for your help Nicolas Schmit

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

その他の回答 (3 件)

Yifan Ma
Yifan Ma 2018 年 2 月 6 日
What you are looking for is map container. https://de.mathworks.com/help/matlab/ref/containers.map-class.html

Luca
Luca 2020 年 3 月 31 日
編集済み: Luca 2020 年 3 月 31 日
Hi,
I have 2 vector (x, y) and I desire math expression y=f(x)
x must be syms. How to?
tnks
  2 件のコメント
Ajay Goyal
Ajay Goyal 2020 年 3 月 31 日
編集済み: Ajay Goyal 2020 年 3 月 31 日
Hi,
As much I did understood your query, you wish to establish relation as y=f(x) and values of x are not known. To answer, I believe, it is not possible as the values of x and y is the first necessity to develop a function. Then only you may use it to predict y for input values of x.
Contrary, if you just want to define a function, you may do it following way:
%%%%Main File:
syms x
[ y ] = Untitled2( x )
%%%%Function file:
function [ y ] = Untitled2( x ) %%%%same name as in main file. Func. file will be stored with this name
y=x.^2; %%%%wrtite the function. Say
end
Kindly let me know, if your query has been resolved.
Best Luck
Ajay
Ajay Goyal
Ajay Goyal 2020 年 3 月 31 日
both files has to be in the working folder

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


Luca
Luca 2020 年 3 月 31 日
編集済み: Luca 2020 年 3 月 31 日
For you.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by