フィルターのクリア

Symbolic Toolbox Solving for Zeros

47 ビュー (過去 30 日間)
Ryan Coder
Ryan Coder 2022 年 12 月 15 日
コメント済み: Paul 2022 年 12 月 15 日
Hey! I've tried a few things but cant seem to get this script to solve for the x values of a functions values. Any ideas?
%% function
function [zeros] = dPlotInfo(func,range)
%
%This function plots a function and its 1st and 2nd derivatives
%It also returns any zeros
%
figure(1);
fplot(func,range,1);
hold on;
fplot(diff(func,1),range);
fplot(diff(func,2),range);
syms x;
zeros = fzero(func == 0 ,range);
%AHHHHH
end
%% test script used for the function
range = [-5 5];
syms x;
f = sin(3*x);
z = dPlotInfo(f, range);
disp(z);

回答 (2 件)

Paul
Paul 2022 年 12 月 15 日
Hi Ryan,
fzero is not used in the Symbolic Math Toolbox. Instead, check into using solve or vpasolve for this problem.
  2 件のコメント
Ryan Coder
Ryan Coder 2022 年 12 月 15 日
I did attempt to use solve before fzero but I couldn't get it to work, much less over a specific range. Could you give me an example?
Paul
Paul 2022 年 12 月 15 日
Here is one way to use solve, taken nearly verbatim from its doc page
syms x real
f(x) = sin(3*x);
Solve the equation, parameterically if needed. If the solution(s) isn't parametertized, then it can be be checked manually to determine if it's in the desired range, which isn't the case here.
[solx,parameters,conditions] = solve(f(x),'ReturnConditions',true)
solx = 
parameters = 
k
conditions = 
Now solve for the parameter that forces the solution to lie within the desired range
assume(conditions)
restrictions = [solx > -5 , solx < 5];
solp = solve(restrictions,parameters)
solp = 
And sub those parameters back into the solution:
valx = subs(solx,parameters,solp)
valx = 

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


Star Strider
Star Strider 2022 年 12 月 15 日
One approach —
range = [-5 5];
syms x;
f(x) = sin(3*x);
z = dPlotInfo(f, range);
Warning: The tolerance parameter input to fplot has been removed.
disp(z);
function [zeros] = dPlotInfo(func,range)
%
%This function plots a function and its 1st and 2nd derivatives
%It also returns any zeros
%
digits(5) % Set Precision On Returned Symbolic Numbers (Convenience)
figure(1);
hfp = fplot(func,range,1);
hold on;
fplot(diff(func,1),range);
fplot(diff(func,2),range);
syms x;
zix = find(diff(sign(hfp.YData))); % Approximate Indices Of Zero-Crossings
for k = 1:numel(zix)
zeros(k) = vpasolve(func, x, hfp.XData(zix(k))); % Use The X-Values For Each Approximate Index Value As A Starting Value
end
%AHHHHH
end
.

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by