現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Substituting and evaluating function
5 ビュー (過去 30 日間)
古いコメントを表示
I have attempted to substitute the X variable into my function and evaluate it. This is the result. If it is not possible please tell me and I will find a difference method?
>> subs der(140,x);
Warning: Support of character vectors that are not valid variable names or define
a number will be removed in a future release. To create symbolic expressions,
first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1586)
In sym>convertChar (line 1491)
In sym>tomupad (line 1243)
In sym (line 199)
In subs (line 66)
>> eval der
der =
(13459*cos((313*x)/200 - 1689/2000))/25 + (42471*cos((1573*x)/1000 + 1153/500))/80 + (937443*cos((1849*x)/2500 + 441/250))/62500
GS.
2 件のコメント
採用された回答
Star Strider
2017 年 9 月 13 日
Try this (in R2012a and later):
syms x
X = sym(140);
der(x) = (13459*cos((313*x)/200 - 1689/2000))/25 + (42471*cos((1573*x)/1000 + 1153/500))/80 + (937443*cos((1849*x)/2500 + 441/250))/62500;
Desired_Symbolic_Result = der(X)
Desired_Numeric_Result = vpa(der(X))
Desired_Symbolic_Result =
(937443*cos(26327/250))/62500 + (42471*cos(111263/500))/80 + (13459*cos(436511/2000))/25
Desired_Numeric_Result =
-503.67380291547475440432522297924
Beginning in R2012a, you can create symbolic functions. You then work with them as you would any other function.
24 件のコメント
Star Strider
2017 年 9 月 13 日
As always, my pleasure!
Also, if you define ‘f(x)’ as the original function definition, the derivative:
der = diff(f, x)
will automatically be a function of ‘x’ as well, as ‘der(x)’.
Gavin Seddon
2017 年 9 月 20 日
Hello again when I try to integrate this, it requires a handle. Is there a way of converting this? As always, thank you. GS.
Star Strider
2017 年 9 月 20 日
You should be able to integrate it symbolically as written.
If you want to integrate it numerically, you need to convert it to an anonymous function to create a function handle for the numeric integration routines. [See the documentation on Anonymous Functions (link) for details.] The easiest way to convert a symbolic function to an anonymous function is to use the matlabFunction (link) function. You can then use it as an argument to integral and other functions.
As always, my pleasure.
Gavin Seddon
2017 年 9 月 26 日
Hello Star Strider when I try with the function I get INT=integral(fx, 138.2, 306.3) Error using integral (line 82) First input argument must be a function handle. I must use your the solution, thanks. GS.
Star Strider
2017 年 9 月 26 日
If you are using an anonymous function, this will work:
INT=integral(fx, 138.2, 306.3)
If you are using a function file, you have to precede the function name with ‘@’ to create a function handle:
INT=integral(@fx, 138.2, 306.3)
I am assuming here that ‘fx’ is a function of only one variable.
Stephen23
2017 年 9 月 27 日
Hello Star Strider, it doesn't appear to like @
>> INT=integral(@344*sin((313*x)/200 - 1689/2000) + (675*sin((1573*x)/1000 + 1153/500))/2 + (507*sin((1849*x)/2500 + 441/250))/25,(138.2000, 306.3000))
INT=integral(@344*sin((313*x)/200 - 1689/2000) + (675*sin((1573*x)/1000 + 1153/500))/2 + (507*sin((1849*x)/2500 + 441/250))/25,(138.2000, 306.3000))
↑
Error: Unexpected MATLAB expression.
GS.
Star Strider
2017 年 9 月 27 日
To define an anonymous function, you need to specify the argument(s) to it. (See the documentation on Anonymous Functions (link) for details.)
Also, you need to remove the parentheses around the integration limits.
Try this:
INT = integral(@(x) 344*sin((313*x)/200 - 1689/2000) + (675*sin((1573*x)/1000 + 1153/500))/2 + (507*sin((1849*x)/2500 + 441/250))/25, 138.2000, 306.3000)
INT =
-179.0603
Gavin Seddon
2017 年 9 月 27 日
Hello I see but since the curve is positive why does it have a negative integral?
Star Strider
2017 年 9 月 27 日
The curve is not uniformly positive. It oscillates with a median of 0 in the region of your limits of integration:
fcn = @(x) 344*sin((313*x)/200 - 1689/2000) + (675*sin((1573*x)/1000 + 1153/500))/2 + (507*sin((1849*x)/2500 + 441/250))/25;
x = linspace(138.2000, 306.3000, 10000);
figure(1)
plot(x, fcn(x))
grid
Gavin Seddon
2017 年 9 月 28 日
Thank you I see this oscillation. Also, the curve is negative yet it lies in the upper right quadrant of a graph. This is a caveat I have never considered. This is probably obvious to a mathematician, would it be useful to investigate this further? Thank you. GS.
Star Strider
2017 年 9 月 28 日
My pleasure.
I am not certain what you are doing, so I cannot offer specific advise. If this result seems anomalous, I certainly believe it would be useful to investigate it further.
Gavin Seddon
2017 年 10 月 5 日
Hello again star Strider, I attempted to repeat this using V rather than der. The experiment has been modified. I received x = sym(170); >> V(x) Index exceeds matrix dimensions.
Error in sym/subsref (line 841) R_tilde = builtin('subsref',L_tilde,Idx); is the problem with my new data? My derivative is V =
- (92331455826879074318081118756173912512776418660041743993904822041876030910119699033499897171126899241725885130346424189784141742262968992408634746136851205869206855701491548798776503105475464220864413217341150170733739807706803033452925986295591352038336042192009955717827037497429499505042435549895145517335007476391376729481782080924433672184175718973072212272070882428631432519752545772508001320679085483283680177407432685533740793358744338287889355782389262329440622613778812194054590418858651122555880316120798011977434246121618976107398197182996875055142408909914155811962311415589065766422076177131304795913721217905037606990771319411*sin((5711*x)/1000 + 1311/1000))/4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\\\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - (337938395605967118783451634120738357644051700575612507073379768863114967513255762797583614901123289513591827402052213060331395907562169969372033371964730098396975697478108635966708436699058012044737559749756894340141369730367633557437286745305395321673936415250385277680481135281460168325144813236682857015303680560921747257877841034623426231273488280813978055066545147906149766878637431325144908837020836874797052493218084299864947857717069041466310745423155285220128581051726929008324822350838304920263403088017826628931947265450765070760842430314065864765854552193925097073647350353333456305726858725752935749441754238330833135163923598606429196573691137345948092087*sin((8111*x)/10000 - 883/500))/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\\\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - (2238214047960166868161666252374168002793633548200148771059904342010973189983069129042552287816045896404475905208516336659925687588964398230729666771262646881929744852332949180098520764229813389827908818003073070342095525455259306860245544499102528592805335810658944666253298566712317210412850678800593773597510804486447294828337350042527773540989757641865746337986161160684427430332725995301245617054848260084662737\\\n87587246990390362423046596214893834190927466438691935825680357272020798301699967083322502229500662422058286719988554418829338360993498526742839202506164100967762155030779279334189661438913028319732962082067372573160136054523940031056116221737040603*sin((7587*x)/10000 + 1401/1000))/500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
GS.
Star Strider
2017 年 10 月 5 日
‘V’ seems to be a (4x1) vector, so calling element 170 is certainly going to throw that error.
You probably need to create ‘V’ as a symbolic function. You can either define it directly, or by using the symfun function. (That applies to R2012a and later. In earlier versions use the subs function.)
Gavin Seddon
2017 年 10 月 5 日
Hello yes I use this first but it did not evaluate the result: subs(V,x,170)
ans =
- (92331455826879074318081118756173912512776418660041743993904822041876030910119699033499897171126899241725885130346424189784141742262968992408634746136851205869206855701491548798776503105475464220864413217341150170733739807706803033452925986295591352038336042192009955717827037497429499505042435549895145517335007476391376729481782080924433672184175718973072212272070882428631432519752545772508001320679085483283680177407432685533740793358744338287889355782389262329440622613778812194054590418858651122555880316120798011977434246121618976107398197182996875055142408909914155811962311415589065766422076177131304795913721217905037606990771319411*sin((5711*x)/1000 + 1311/1000))/4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\\\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - (337938395605967118783451634120738357644051700575612507073379768863114967513255762797583614901123289513591827402052213060331395907562169969372033371964730098396975697478108635966708436699058012044737559749756894340141369730367633557437286745305395321673936415250385277680481135281460168325144813236682857015303680560921747257877841034623426231273488280813978055066545147906149766878637431325144908837020836874797052493218084299864947857717069041466310745423155285220128581051726929008324822350838304920263403088017826628931947265450765070760842430314065864765854552193925097073647350353333456305726858725752935749441754238330833135163923598606429196573691137345948092087*sin((8111*x)/10000 - 883/500))/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\\\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - (2238214047960166868161666252374168002793633548200148771059904342010973189983069129042552287816045896404475905208516336659925687588964398230729666771262646881929744852332949180098520764229813389827908818003073070342095525455259306860245544499102528592805335810658944666253298566712317210412850678800593773597510804486447294828337350042527773540989757641865746337986161160684427430332725995301245617054848260084662737\\\n87587246990390362423046596214893834190927466438691935825680357272020798301699967083322502229500662422058286719988554418829338360993498526742839202506164100967762155030779279334189661438913028319732962082067372573160136054523940031056116221737040603*sin((7587*x)/10000 + 1401/1000))/500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 GS
Star Strider
2017 年 10 月 5 日
One problem is that you have ‘\\\n’ embedded in that expression, every 1000 or so characters.
However, when I manually delete those and then execute this:
V =
NaN
Vx = subs(V,x,170)
Vx =
NaN
Note that the arguments to trigonometric functions in the Symbolic Math Toolbox are in radians, not degrees.
For whatever reason, your function initially evaluates to NaN, possibly because of the magnitudes of the constants. I leave it to you to sort that.
Gavin Seddon
2017 年 10 月 25 日
Hello Star Strider I am integrating the function however how do I simplify my new symbolic variable because it provides an equation rather than a number for example: INT=int(fx,160,303)
INT =
(968700*cos(12801/100))/8111 + (1094000*cos(122793/1000))/7587 - (1055*cos(216468/125))/11422 + (1055*cos(915071/1000))/11422 - (1094000*cos(2312871/10000))/7587 - (968700*cos(2439973/10000))/8111
>> eval INT
INT =
(968700*cos(12801/100))/8111 + (1094000*cos(122793/1000))/7587 - (1055*cos(216468/125))/11422 + (1055*cos(915071/1000))/11422 - (1094000*cos(2312871/10000))/7587 - (968700*cos(2439973/10000))/8111 GS.
Steven Lord
2017 年 10 月 25 日
In general you should avoid eval. To convert that symbolic result into a numeric result you can use either double or vpa. If your result had included a symbolic variable, either vpa or subs (to substitute a value for the symbolic variable(s)) should work.
Gavin Seddon
2017 年 11 月 2 日
Hello again, I have shown an increase in the negative integral. However rather than repeat linespace is it possible to plot a histogram around a median? GS.
Star Strider
2017 年 11 月 2 日
I am not following what you are doing.
You can specify the histogram edges to be symmetric about the median.
Experiment with this to define the edges:
mdn = 3; % Median Of Data
N = 12; % Number Of Bins Desired
edgvct = linspace(-N/2, N/2, N+1) + mdn; % Vector Of Bin Edges
Gavin Seddon
2017 年 11 月 13 日
Hello, this is really trivial however histogram(X) does not use my values. So how do I instruct Matlab to use my X and Y values? GS.
Star Strider
2017 年 11 月 13 日
The documentation provides guidance:
histogram(X,edgvct)
or if you have two arrays and you want a 2-dimensional histogram, use the histogram2 function. You may have to provide a second vector of edges for the ‘Y’ data as well. See the documentation for details.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)