Programmatically find fzero of two RCL functions
Ältere Kommentare anzeigen
How do I fix my code to programmatically find the fzero of two functions? I had to hand-code the functions funcVL and funcVR from model fit coefficients. I want to do that programmatically, then find fzero (intersection point) of the two function curves. Need some help here. Thank you
% find intersection (frequency) of two RCL functions
load RCL_vars.mat % creates 3 vectors, f, VL, VR from data
% function calls
createFitVL(f, VL)
createFitVR(f, VR)
% find intersection point, f
x = f;
funcVL = @ (x) -3.948*exp(-0.658*x)+3.898;
funcVR = @ (x) 3.856*exp(0.2436*x)+0.2436;
%use fzero with the function: @(x) funcVL(x) - funcVR(x).
soltn = fzero(@(x) funcVR(x)- funcVL(x), [0, 10])
%%
function [fitresult, gof] = createFitVL(f, VL)
%CREATEFIT(F,VL)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input: f
% Y Output: VL
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 26-Feb-2025 15:57:28
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( f, VL );
% Set up fittype and options.
ft = fittype( 'a*exp(-b*x)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.317099480060861 0.950222048838355 0.0344460805029088];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'VL vs. f', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'f', 'Interpreter', 'none' );
ylabel( 'VL', 'Interpreter', 'none' );
grid on
end
%
hold on
%% Fit: 'untitled fit 1'.
function [fitresult, gof] = createFitVR(f, VR)
[xData, yData] = prepareCurveData( f, VR );
% Set up fittype and options.
ft = fittype( 'a*exp(-b*x)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.257508254123736 0.840717255983663 0.254282178971531];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'VR vs. f', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'f', 'Interpreter', 'none' );
ylabel( 'VR', 'Interpreter', 'none' );
grid on
end
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




