이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Substituting and evaluating function
조회 수: 6 (최근 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)