matlab代码

时间:2016-11-09 10:52:39

标签: matlab

我更新了问题以澄清更多内容。这是一张图表:

对于附图中的曲线,我希望绘制曲线。我有它的等式,它是简化后将像这一个

% Eq-2
    (b*Y* cos(v) + c    -  k*X*sin(v))^2 + ...
sqrt(k*X*(cos(v) + 1.0) +  b*Y*sin(v))^2) - d = 0.0 

其中:

v = atan((2.0*Y)/X) + c 

bcdk是常量。

来自附图,

曲线分为两点:

p1 @ (x=0)
p2 @ (y=0)

如果我的问题不明确,请接受我的道歉。

由于

2 个答案:

答案 0 :(得分:1)

因此,在您编辑之后,您会更清楚自己想要什么。

我坚持认为你的等式需要工作 - 原始等式(在编辑之前)简化为我下面的内容。除了XY截距位于不同的位置之外,其曲线看起来就像你的情节一样,并且在X = 0附近发生了时髦的事情,因为你有切线的数值问题(你可能想要重新解决问题)。

但是,在检查了您的等式后,以下代码应该会有所帮助:

function solve_for_F()

    % graininess of alpha
    N = 100;

    % Find solutions for all alphae
    X       = zeros(1,N);
    options = optimset('Display', 'off');    
    alpha   = linspace(0, pi/2, N);
    x0      = linspace(6, 0, N);

    for ii = 1:numel(alpha)    
        X(ii) = fzero(@(x)F(x, alpha(ii)), x0(ii), options);
    end

    % Convert and make an X-Y plot
    Y = X .* tan(alpha);

    plot(X, Y,...
        'linewidth', 2,...
        'color',     [1 0.65 0]);

end

function fval = F(X, alpha)

    Y = X*tan(alpha);

    % Please, SIMPLIFY in the future
    A = 1247745517111813/562949953421312;
    B = 4243112111277797/4503599627370496;
    V = atan2(2*Y,X) + A;

    eq2 = sqrt(  (5/33*( Y*sin(V) + X/2*(cos(V) + 1) ))^2 + ...
                 (5/33*( Y*cos(V) - X/2* sin(V)      ))^2  ) - B;

    fval = eq2;

end

结果:

plot results

答案 1 :(得分:0)

所以,我很开心(谢谢你)!

不同的问题,不同的答案。

下面的解决方案首先搜索导致您正在寻找的XY拦截的常量(p1p2)。对于那些最适合问题的常数,它会考虑数值问题制作情节。

事实上,你不需要eq。 1,因为对于任何曲线来说都是如此 - 它只是让你迷惑,并且使用上有问题。

所以,这是:

function C = solve_for_F()

    % Points of interest
    px = 6;
    py = 4.2;

    % Wrapper function; search for those constants 
    % causing the correct X,Y intercepts (at px, py)
    G = @(C) abs(F( 0, px, C)) + ... % X intercept at px
             abs(F(py,  0, C));      % Y intercept at py

    % Initial estimate, based on your original equation
    C0 = [5/33
          1247745517111813/562949953421312
          4243112111277797/4503599627370496
          5/66];

    % Minimize the error in G by optimizing those constants
    C = fminsearch(G, C0);

    % Plot the solutions
    plot_XY(px, py, C);

end

function plot_XY(xmax,ymax, C)

    % graininess of X
    N = 100;

    % Find solutions for all alphae
    Y       = zeros(1,N);
    X       = linspace(0, xmax, N);
    y0      = linspace(ymax, 0, N);
    options = optimset('Display', 'off',...,...
                       'TolX'   , 1e-10);

    % Solve the nonlinear equation for each X
    for ii = 1:numel(X)

        % Wrapper function for fzero()
        fcn1 = @(y)F(y, X(ii), C);

        % fzero() is probably the fastest and most intuitive 
        % solver for this problem
        [Y(ii),~,flag] = fzero(fcn1, y0(ii), options);

        % However, it uses an algorithm that easily diverges
        % when the function slope is large. For those cases, 
        % solve with fminsearch()
        if flag ~= 1

            % In this case, the minimum of the absolute value
            % is searched for (which should be zero)
            fcn2 = @(y) abs(fcn1(y));

            Y(ii) = fminsearch(fcn2, y0(ii), options);
        end

    end

    % Now plot the X,Y solutions
    plot(X, Y,...
        'linewidth', 2,...
        'color',     [1 0.65 0]);    
    xlabel('X'), ylabel('Y')
    axis([0 xmax+.1 0 ymax+.1])

end

function fval = F(Y, X, C)

    % Unpack constants
    b = C(1);  d = C(3);
    c = C(2);  k = C(4);

    % pre-work
    V = atan2(2*Y, X) + c;

    % Eq. 2
    fval = sqrt( (b*Y*sin(V) + k*X*(cos(V) + 1))^2 + ...
                 (b*Y*cos(V) - k*X* sin(V)     )^2  ) - d;
end

solution