starbinbin_csu 发表于 2010-9-12 17:16:56

关于一个积分的matlab代码

R=sqrt(1.625^2-(Hr*cos(alpha)-1.625)^2);
            h=R-sqrt(R^2-Hr*(3-Hr));
            S=(h-R)*sqrt(2*h*R-h^2)+R^2/2*asin((h-R)/R)+pi/2*R^2;
            v2=int(S,Hr,-6*tan(alpha)+(Hr-1.5)*cos(belta)-1.5)+1.3*pi;上式出现在一个function 中,以上式子所有的变量都通过function传参进行赋值,也就是说只有被积分变量Hr是不能直接赋值的,可是Hr在function中要如何定义呢?
如果用syms定义就会报错:
??? Error using ==> assignin
Attempt to add "Hr" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
可是不定义积分就无法进行,请问各位有什么办法吗?

qibbxxt 发表于 2010-9-12 17:29:12

1.没有原问题,很难找出错误
2.符号积分比较麻烦,有些未知的错误
3.建议你用数值积分,quad或者quad2d等

starbinbin_csu 发表于 2010-9-12 17:33:20

2# qibbxxt
后来仔细分析了一下,就是像以下方式的syms定义方式都会报错,是什么原因呢?function tests()
test2();
syms x;
    function test2()
      f=x^2;
      a=int(f,1,2);
      disp(a);
    end
end或者
function tests()
test2();
    function test2()
      syms x;
      f=x^2;
      a=int(f,1,2);
      disp(a);
    end
end

qibbxxt 发表于 2010-9-12 20:29:02

本帖最后由 qibbxxt 于 2010-9-12 21:21 编辑

3# starbinbin_csu
我的电脑上面没有符号工具箱,还有没法去调试
建议你不要用内嵌函数,改用子函数的形式试一试
另外检查一下你的matlab的符号工具箱是不是全的

starbinbin_csu 发表于 2010-9-12 21:54:25

后一个end是父函数的end,这个end是可以不加的,但是在子函数很多的情况下感觉还是加了会比较直观点。

qibbxxt 发表于 2010-9-12 22:00:18

5# starbinbin_csu
那倒数第二个end是什么,这样写就是内嵌函数
建议你查一查子函数和内嵌函数的区别
然后改成子函数试一试

bainhome 发表于 2010-9-12 22:01:34

不用看了,符号函数的变量定义有误:function tests()
test2();
    function test2()
      x=sym('x')
      f=x^2;
      a=int(f,1,2);
      disp(a);
    end
end刚才瞅错了,没看见是nested function,贸然写了个帖子,又悄悄删掉了。

starbinbin_csu 发表于 2010-9-12 22:52:13

7# bainhome
请问这种定义方式和直接用syms 定义有什么区别呢?

bainhome 发表于 2010-9-12 23:32:10

本质上没什么区别,看出错提示估计是nested function要用到evalin和assignin两个命令在workspace里来回I/O变量,而这两个命令是老命令了,基本等于把字符串读成一个表达式,这种方式也许对syms 和x之间的那个空格比较困惑吧。
属于个人猜测。

rocwoods 发表于 2010-9-13 14:22:41

本帖最后由 rocwoods 于 2010-9-13 14:25 编辑

在帮助文档中找到下面的说明:
MATLAB issues an error if you attempt to dynamically add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function. Examples of operations that might use dynamic assignment in this way are shown in the table below.

Type of Operation
Evaluating an expression using eval or evalin, or assigning a variable with assignin      

How to Avoid Using Dynamic Assignment

As a general suggestion, it is best to avoid using the eval, evalin, and assignin functions altogether.
syms函数调用了assignin函数,因此会出错。这样也是会出错的:
function tests
%Just contains a nested function and does not call it
syms y;
    function test2()
       %nothing to do
    end
end
页: [1]
查看完整版本: 关于一个积分的matlab代码