使用"subs"评估“溶解"输出的功能.在Maltab提供额外的输出 [英] Using "subs" Function to Evaluate Output of "dsolve" Give Extra Output in Maltab

查看:183
本文介绍了使用"subs"评估“溶解"输出的功能.在Maltab提供额外的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您想了解宏伟计划,请阅读介绍.如果不是,只需跳至我的问题.

我有一个关于微分方程和线性代数课程的项目,在这个项目中,我必须使用计算机代数系统来求解具有常数系数的一阶线性,常微分方程和具有常数系数的非线性ODE.我必须证明这是通过分析和数字方式完成的.我的计划是要有两个函数,其中dsolve函数用于分析部分,而另一个函数ODE1是我通过

I have a project for my Differential Equations and Linear Algebra course where I have to use a computer algebra system to solve both linear, ordinary differential equations of the first degree with constant coefficients and a non-linear ODE with constant coefficients. I have to show this being done both analytically and numerically. My plan is to have 2 functions that make use of the dsolve function for the analytical part and a function called ODE1 I learned about through this tutorial by Matlab. This function is defined to approximate the solution to the differential equation using Euler's Method. The functions would all use the same input parameters so each input could be defined once and the functions would all understand what inputs to use (maybe nest the functions under one calling function). The goal is to evaluate the analytical solution over the same interval being used to find the numerical approximation and compare the results in both a table and graph. I got the numerical solution to give me a "table" in the form of a row vector and also graph that row vector. I began having an issue with the Analytic solution...

为求解一阶线性ODE,我生成了此函数

To solve a linear ODE of the first order, I generated this function

function [s1] = L_Analytic(eqn,t0,h,numstep,y0)

% eqn is the differential equation to be solved
% t0 is the start of the interval that the resulting equation is to be evaluated at
% h is the stepsize
% numstep is the number of steps
% y0 is the initial condition

syms y(x)
cond = y(0) == y0;
A = dsolve(eqn,cond);
s1 = A;
S1 = s1;
   for t = t0 : h : h*(numstep-2)
       S1 = [subs(S1); vpa(subs(s1))]
   end 
end

此函数L_Analytic(diff(y)==y, 0, 0.1, 5, 1)生成的列表为

1
1.0
1.105170...
1.221402...
1.349858...

当使用相同的输入在Matlab中的不同函数中使用数值方法时,得到以下列表:

When using the numerical method in a different function in Matlab using the same inputs, I get the list:

1.0000
1.1000
1.2100
1.3310
1.4641

对于那些了解微分方程式方法或精通微积分的人,y'= y的解为e ^ x,并且使用5步在区间0:0.4上进行评估时,列表应为

For those who know their way around differential equations or are proficient in calculus, the solution to y' = y is e^x, and when evaluated over the interval 0:0.4 using 5 steps, the list should be

1
1.105...
1.2214...
1.3498...
1.4918...

四舍五入后

所以这里的问题是我的分析解决方案中有一个额外的1条目.我相信这与S1 = [subs(S1); vpa(subs(s1))]中的subs(S1)部分有关for循环,但是我对如何解决这个问题感到困惑.

So the issue here is that I have an extra entry of 1 in my analytical solutions. I'm confident it has something to do with the subs(S1) part of S1 = [subs(S1); vpa(subs(s1))] in the for loop but I am stumped as to how to fix this.

我有点理解为什么我需要使用subs函数,因为我正在使用符号变量来使用dsolve函数,该函数在其答案中输出符号变量.另外,为了使for循环迭代和更改,必须每次将符号变量替换为t的实际值.我确实尝试将vpa(subs(s1))移到for循环之前,但这只是在向量中返回了相同的值5次.我也尝试不使用subs(S1),它给了我

I kind of understand why I need to use the subs function, in that I am using symbolic variables to use the dsolve function which outputs symbolic variables in its answer. Also, in order for the for loop to iterate and change, the symbolic variables must be substituted for real values of t each time. I did try moving the vpa(subs(s1)) just before the for loop, but this just returned the same value in the vector 5 times. I also tried not using subs(S1) and it gave me

exp(t)
1.0
1.1051709...
1.2214027...
1.3498588...

所以我很肯定这是代码的这一部分.

so I'm positive it's this part of the code.

侧面说明:我理解分析方法会像在链接的视频中显示的ODE1一样输出列向量.为了使Matlab将其绘制为一条线,我将列向量转置为行向量,并在解决方案部分固定后对解析解决方案进行同样的处理.

Side Note: I understand the analytical method outputs a column vector as does the ODE1 shown in the video that's linked. In order to have Matlab plot this as one line, I transposed the column vector to make a row vector and will do the same with the analytical solution once the solution part is fixed.

推荐答案

通过更改for循环的内部结构,我可以使其正常工作.我最终的功能代码是这样的: 函数[s1] = L_Analytic3(eqn,t0,h,numstep,y0)

By changing the internals of the for loop I made it work. My final function code turned out to be this: function [s1] = L_Analytic3(eqn,t0,h,numstep,y0)

%Differential Equation solver for specific inputs
%   eqn is the differential equation
%   t0 is start of evaluation interval
%   h is stepize
%   numstep is the number of steps
%   y0 is the initial condition

syms y(x)
cond = y(0) == y0;
A = dsolve(eqn, cond);
s1 = A;
S1 = s1;
for x = t0 : h : h*(numstep)
    subs(x);
    if x == t0    
        S1 = subs(s1,x);
    else 
      S1 = [subs(S1), subs(s1,vpa(x))];
    end
end
end

这篇关于使用"subs"评估“溶解"输出的功能.在Maltab提供额外的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆