How to write a symfun for given Y values array?

Hi guys.
  1. The temputure array A is given (Y value) in hours,
  2. of course, X are uniformly distributed in hours, e.g x1 = 1, x2 = 2, x3 = 3. x4 = 4 ...
  3. e.g a1 = f(x1=1), a2 = f(x2=2), a3 = f(x3=3), ...
My question is, how to write this weather vs hour function ?
Note that, 1. this funcation must be a symfun type, (f is symfun, x is sym) because I need to give this symfun into the "int" function later.
2. since this funcation will feed into "int", I think it should be able to give any Y value for given X. e.g, Y = whatever number = f(1.64), this 1.64 is a number between x1 and x2, in other words, this function is not discrete (not spikes) but a continuous. It doesnt need to be "smoothly" continuous, even straight line connected is fine.
Thank you!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 8월 19일

0 개 추천

I do not understand your point about needing a symfun?
The difference between int() of a symbolic expression() compared to int() of a symfun, is:
  • the result of int() of a symbolic expression is always a symbolic expression. If no integration variable is specified, then symvar() is used to find the default variable to integrate with respect to
  • the result of int() of a symfun is not always a symfun. If no integration variable is specified, then the variable that is the first parameter is used as the variable to integrate with respect to. The variable that was integrated with respect to is removed from the parameter list of the symfun being integrated; if there are no remaining parameters, then the result of the int() is a symbolic expression, but if there is at least one remaining parameter of the original symfun after removing the variable, then the result is a symfun of the remaining variables, in the same order as in the original symfun
Otherwise there is no difference. int() of symfun() proceed by determining the variable to integrate with respect to, stripping the symfun layer off and doing int() of the resulting symbolic expression, and then wrapping the result in a symfun if there are any variables left in the original parameter list.
Using a symfun can certainly be convenient for some uses, saving having to explicitly subs() a value for a variable, but it integrates just like symbolic expressions do with the minor change of how it determines the integration variable if one is not specified

댓글 수: 7

If your vector of given temperature values is numeric, then you can polyfit() it and then poly2sym() the resulting parameters to get a predictive function.
If your vector of given temperature values is not numeric, then https://www.mathworks.com/help/symbolic/sym.chebyshevt.html
sun
sun 2021년 8월 20일
편집: sun 2021년 8월 20일
Hi Walter, thanks so much for your reply. Let me put it into a simple way.
  1. temperature values is numeric, they are numbers of a row. I do tried use the way you suggested, polyfit() it and then poly2sym(). However, I got error "Unable to convert expression containing remaining symbolic function calls into double array. Argument must be expression that evaluates to number.", do you know how to solve it ?
  2. In the stuff what I am doing, this function was writen by using a "piecewise" function. I saw this piecewise function is symfun, that's why I need this new function as to be symfun. I was using piecewise function to write this weather function in a very "stupid" way (as below) to test my code.
piecewise(0<=hour<356,30,366<=hour<720,35);
Now my code is working, I need to replace this piecewise function. because my temperature values are using much higher resolution.
a = randi([50 90], 1, 20);
t = 1 : length(a);
P = poly2sym(polyfit(t, a, 3))
P = 
I saw this piecewise function is symfun, that's why I need this new function as to be symfun.
No. You might possibly require piecewise(), but you only need it to be a symfun() if you need to be able to invoke the result in function form -- which is not the case if you are passing to int(). int() will strip out the function layer, using it only as hints about what the default variable is.
int() of piecewise constants over the whole range covered by the conditions, is sum of the constants times the length of their respective field. If you are taking int() over a smaller range, then discard any unused conditions and adjust the boundaries as needed on what remains.
sun
sun 2021년 8월 23일
Hi walter, thanks for much for your help again.
I believe this polyfit() and poly2sym() will give me the correct function (or input) for given temputure array, thank you for that. However, I still have issue of "Unable to convert expression containing remaining symbolic function calls into double array. Argument must be expression that evaluates to number".
I listed a new question about it, since I feel like you are the master, If I may, could you please take a look at this question ? It's much more straightforward.
(The function turned out to have a discontinuity inside the integration limits)
sun
sun 2021년 8월 24일
@Walter Roberson I just noticed that, this polyfit does not work at all. If you check the pic below, you will see, the output of poly does NOT like the orignal temp at all. Do you have any suggestion plz?

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

sun
2021년 8월 19일

댓글:

sun
2021년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by