割线根查找器问题

时间:2018-02-23 22:15:57

标签: matlab variables for-loop

我不确定如何在三个变量之间移动/复制数据:

scenarios = {
    1: {a: 'x', b: 1, c: 'xx'},
    2: {a: 'y', b: 1, c: 'xx'},
    3: {a: 'z', b: 1, c: 'xx'},
    4: {a: 'x', b: 2, c: 'xx'},
    5: {a: 'y', b: 2, c: 'xx'},
    6: {a: 'z', b: 2, c: 'xx'},
    7: {a: 'x', b: 3, c: 'xx'},
    8: {a: 'y', b: 3, c: 'xx'},
    9: {a: 'z', b: 3, c: 'xx'},
    .. }

这是我的代码:

       async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Publication p = (Publication)e.SelectedItem;
        Debug.WriteLine(p);
        if (p.folderID.Equals("-1"))
        {
            using (Stream respStream = await post(p.docNum))
            {
                string ext = p.appextension.ToLower();
                byte[] buffer = new byte[respStream.Length];
                respStream.Read(buffer, 0, buffer.Length);
                string path = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/downloadedFile." + ext;
                File.WriteAllBytes(path, buffer);
                switch (ext)
                {
                    case "pdf":
                        await Navigation.PushAsync(new PDFViewPage(path));
                        break;
                    case "docx":
                        Device.OpenUri(new Uri(path));
                        break;
                    default:
                        Debug.WriteLine("wasn't pdf");
                        Debug.WriteLine("was a ." + ext);
                        break;
                }


            }
        }
        else
        {
            await Navigation.PushAsync(new PublicationsPage(p.folderID));
        }
    }

    private async Task<Stream> post(string id)
    {
        Dictionary<string, string> dir = new Dictionary<string, string>();
        dir.Add("LoginID", App.user.login_id);
        dir.Add("docID", id);
        var jsonReq = JsonConvert.SerializeObject(dir);
        Debug.WriteLine("req: " + (String)jsonReq);
        var content = new StringContent(jsonReq, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        var responseString = await response.Content.ReadAsStreamAsync();
        return responseString;
    }
}

1 个答案:

答案 0 :(得分:0)

您的多项式看起来是x^3 - 2x^2 - 4x + 8的形式。让我们首先说一下在Matlab中找到根源的更简单的方法,例如使用roots function如下:

x = -100:100;
y = (x.^3) - (2 .* x.^2) - (4 .* x) + 8;
r = roots([1 -2 -4 8])

r =
    -2.00000000000000
     1.99999998974515
     2.00000001025485

无论如何,让我们专注于割线方法。您应该定义的第一件事是:

  • 您的潜在结果可以绑定的容差级别,以检查是否已到达根目录(例如1e-6
  • 用于表达多项式的单个匿名函数,在本例中为f = @(x) x^3 - 2*x^2 - 4*x + 8;

完成后,您可以构建整个脚本:

clear();
clc();

com = Inf;
i = 2;
n = 100;
tol = 1e-6;

f = @(x) (x^3) - (2*x^2) - (4*x) + 8;

x(1) = input('Low Guess:     '); 
x(2) = input('High Guess:    ');

while ((abs(com) > tol) && (n > 0))
    com = f(x(i))*(x(i)-x(i-1))/ (f(x(i)) - f(x(i-1)));
    x(i+1)= x(i) - com;

    i = i + 1;
    n = n - 1;
end

display(['Root X = ' num2str(x(end))]);

输出结果为:

Low Guess:     0
High Guess:    10
Root X = 2