Matlab 如何使用我用起点编写的代码绘制函数?

Matlab 如何使用我用起点编写的代码绘制函数?,matlab,plot,numerical-methods,Matlab,Plot,Numerical Methods,我编写了割线算法的代码,现在我有了函数: f(x)=2x^3-4x^2+3x,有两个初始点:x0=-1,x1=2 我的问题是如何将我编写的函数(即,割线)与上面的函数,f,以及下面的结果绘制在一个图形中 甚至有可能做到这一点吗 我使用割线算法后得到的结果是: v = -4.0000 2.2069 2.3699 2.6617 2.5683 2.5804 对于上面给定的x0&x1,我在我的割线算法上使用了6次迭代 如果你能解释一下,我将不胜感激

我编写了割线算法的代码,现在我有了函数:

f(x)=2x^3-4x^2+3x
,有两个初始点:
x0=-1,x1=2

我的问题是如何将我编写的函数(即,
割线
)与上面的函数,
f
,以及下面的结果绘制在一个图形中

甚至有可能做到这一点吗

我使用割线算法后得到的结果是:

    v =
   -4.0000
    2.2069
    2.3699
    2.6617
    2.5683
    2.5804
对于上面给定的
x0&x1
,我在我的
割线算法上使用了6次迭代

如果你能解释一下,我将不胜感激

编辑:

这是我用来获得结果的代码:

[z,n,v]=secant([-1,2],10^(-5),10^(-5),10)
对于原型:

function [x,n,v]=secant(X,d,e,N)

% x0 is the first point
% x1 is the end point 
% d is the tolerance 
% e is the requested precision 
% N is the number of iterations 

谢谢。

您可以将函数和结果绘制为散点

首先,以矢量方式定义函数:

f(x) = @(x) ( 2*x.^3 - 4*x.^2 + 3*x );
然后在一定范围内绘制函数:

x = -10:10;
y = f(x);
figure(); plot(x,y);
现在,显示结果:

hold on;
scatter(v,f(v));

我很快就把它拼凑起来,它展示了强大的力量 它向您展示了如何绘制割线函数的结果(与wikipedia上的方法相同:)

但我不明白的是,为什么割线函数既有一个公差,又有一个要求的精度作为输入;我认为公差是割线算法的结果

function [  ] = tmp1(  )

    f=@(x) x.^2;
    [xend,n,v]=secant(f,-4,3,1e-4,50);
    fprintf('after %d iterations reached final x_end = %g, f(x_end) = %g\n',n,xend,f(xend))


    figure;hold on;
    xtmp = linspace(min(v),max(v),250);
    plot(xtmp,f(xtmp),'r'); % plot the function itself
    line([v(1:end-2) v(3:end)]',[f(v(1:end-2)) zeros(n+1,1)]','Color','b','Marker','.','MarkerEdgeColor','b'); % plot the secant lines
    plot(v,f(v),'.','MarkerEdgeColor','r')% plot the intermediate points of the secant algorithm
    line([v(3:end) v(3:end)]',[zeros(n+1,1) f(v(3:end))]','Color','k','LineStyle','--'); % vertical lines

    ylim([-4 max(f(xtmp))]); % set y axis limits for nice plotting view algorithm

end

function [xnew,n,v]=secant(f, x0,x1,e,N)
% x0 is the first point
% x_end is the end point
% e is the requested precision
% N is the number of iterations

v=zeros(N+2,1);
v(1)=x0;
v(2)=x1;

for n=0:N-1
    xnew = x1 - f(x1) * (x1-x0)/(f(x1)-f(x0));
    v(3+n) = xnew;
    if abs(f(xnew)) <e
        break;
    else
        x0=x1;
        x1=xnew;
    end
end
v(n+4:end)=[];

end
函数[]=tmp1()
f=@(x)x.^2;
[xend,n,v]=割线(f,-4,3,1e-4,50);
fprintf('在%d次迭代之后,达到最终的x_端=%g,f(x_端)=%g\n',n,xend,f(xend))
图形等等
xtmp=linspace(最小(v),最大(v),250);
绘图(xtmp,f(xtmp),‘r’;%绘制函数本身
直线([v(1:end-2)v(3:end)],[f(v(1:end-2))零(n+1,1)],“颜色”,“b”,“标记”,“标记颜色”,“b”);%画割线
plot(v,f(v),‘.,‘MarkerEdgeColor’,‘r’)%绘制割线算法的中间点
线条([v(3:end)v(3:end)],[zero(n+1,1)f(v(3:end))],“Color”,“k”,“LineStyle”,“'--');%垂直线
ylim([-4最大值(f(xtmp))];%为nice打印视图算法设置y轴限制
结束
函数[xnew,n,v]=正割(f,x0,x1,e,n)
%x0是第一点
%x_end是终点
%e是要求的精度
%N是迭代次数
v=零(N+2,1);
v(1)=x0;
v(2)=x1;
对于n=0:n-1
xnew=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
v(3+n)=xnew;

如果abs(f(xnew))谢谢,但我认为“y=f(x);”有问题,因为它显示错误。对不起,没关系。非常感谢。还有一个问题,我能把f(x)开头的割线传递给每个结果吗?我想通过向每个结果传递割线来查看结果的进度。。再次感谢!代码中的“xtmp”有问题,因此出现了一些问题,您能看一下吗?谢谢我在编辑过程中不小心删除了它:(sry,我把它放回原处了,恐怕还有一个问题:“使用horzcat CAT参数时出错,维度不一致。”在“line([v(1:end-2)v(3:end)]”的行中,[f(v(1:end-2))…。”非常奇怪的是,c/p’修改了我提供的脚本并重新运行了它,没有出现任何错误。您是否修改了它或使用了割线函数?如果是,请将零(n+1,1)替换为零(长度(v)-2,1)