how to find the min point and plot the graph using secant method
5 次查看(过去 30 天)
显示 更早的评论
the question is asking to find the min point of a function using secant method, the original function is: f(x)= (((sqrt(xi.^2+50^2))*3) + ((100 - xi)*1.85)) my data is like this: the function part:
function [root,iter] = modisecant(f, xi, del, precision)
fxi = f(xi);
Del = xi*del;
fdel = f(xi+Del);
i = 0;
while abs(fxi)>precision
xi = xi - Del*fxi/(fdel-fxi);
fxi = f(xi);
Del = xi*del;
fdel = f(xi+Del);
i = i +1;
end
root = xi;
iter = i;
the main part:
x = linspace(0,100,101)
xi = 20;
del = 0.01;
precision = 0.001;
f = @(xi)(((sqrt(xi.^2+50^2))*3) + ((100 - xi)*1.85))
dcost = modisecant(f, xi, del, precision);
plot(x,dcost,'r')
I don't know what is wrong or I get lost in the variables, just can not the value. thx for helping
0 个评论
回答(2 个)
David Ding
2016-12-29
编辑:David Ding
2016-12-29
Hello Zongyi,
My understanding is that you are unable to determine the minimum of the function and plot the convergence to minimum against iteration steps in your MATLAB script using your "modisecant" function.
-----
There are two issues with solutions/workarounds. Let us look at each one at a time:
1. How to Find the Min Point of the Function
Please note that the Secant Method only determines the roots of the function. It is always a good idea to plot the function whenever possible to obtain a good estimate of the critical points of interest. In this case, in the "funcPlot.pdf" attached, you can see that this function has no real roots, and thus the Secant Method will not converge (given your criteria for convergence in the "precision" variable).
Instead of using the Secant Method, you might find the "fmincon" function in MATLAB helpful in finding a local minimum of a function. If the function you are working with is convex, then any local minimum is also a global minimum of the function.
2. Plot the Graph Using Secant Method
Here I am assuming you want to plot the "cost" function, or how close the function is to the minimum, against the iteration steps. In order to do so, you must pre-allocate an array in MATLAB that stores a vector where each element is the cost at each iteration step. For example, cost = zeros(MAX_NUM_LOOP, 1) is a vector containing MAX_NUM_LOOP elements, in which the kth element will store the cost associated with the method in the kth iteration. Then, simply call plot(cost) to plot the cost vector against the iteration steps.
As a best practice, it is always a good idea to assign a max number of iterations in any "while" loop to prevent the iterations from running forever in the case of divergence.
0 个评论
Rihan Jahngir
2021-3-21
clear all clc f=@(x)x*x*x-4*x-9; %Write your function f(x), where f(x)=0. % Root lies between in the interval (x0, x1). x0=input('\n Enter left point of interval '); %here x0=2 x1=input('\n Enter right point of interval ');%here x1=3 epsilon=input('\n Enter the error '); %error of tolerance you want. for exmple 0.001 or 0.0001 etc. err=abs(x1-x0); %Formula: x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0)); if f(x0)*f(x1)>0 disp('Enter valid interval !!!') else while err > epsilon x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0)); x0=x1; x1=x2; err=abs(x1-x0); root=x2;
end
fprintf('\n The root is %4.3f ',root);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!