声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2801|回复: 15

[编程技巧] inline函数出错问题

[复制链接]
发表于 2007-10-8 09:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
inline函数提示是输入必须为字符串,不过没有看出哪儿错,请教一下各位~~,谢谢
  1. ………………………………
  2.    syms  phis2 phitheta thetas phis;              %定义符号函数:  
  3.    phi2 = q2s * phis;
  4.    phitheta = phis + ( thetas0 + thetas );
  5.    y2 = linspace(y2min,y2max,10);       %产生等差数列,y2为y2min至y2max之间共10个数
  6.    options = optimset('Display','off');

  7. ……………………
  8. for k = 1:10
  9.     ysc2 = y2(k) + (rbs*(sin(phi2)*(sin(phitheta)-thetas*cos(phitheta))+cos(phi2)/(q2s*cos(phitheta))));
  10.     ysc2 = vpa(subs(ysc2),4);
  11.     yfc2 = inline('ysc2');                 %已按花如月建议修改
  12.     thetaslist(k,:) = linspace(thetaslimits(k,1),thetaslimits(k,2),10);  %#ok<AGROW>  %thetas的取值矩阵
  13.     for m = 1:10
  14.         phisnum(k,m) = fzero(yfc2,0,options,thetaslist(k,m));            %#ok<AGROW>  %phis的取值矩阵
  15.     end
  16. end
  17. ………………………………
复制代码
错误提示是:
   ysc2 =-300.+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas)
  
??? Error using ==> inline.inline at 47
Input must be a string.
Error in ==> FaceGear at 58
    yfc2 = inline(ysc2);

[ 本帖最后由 eight 于 2007-10-8 11:10 编辑 ]
回复
分享到:

使用道具 举报

发表于 2007-10-8 10:17 | 显示全部楼层
原帖由 zqllc 于 2007-10-8 09:46 发表
inline函数提示是输入必须为字符串,不过没有看出哪儿错,请教一下各位~~,谢谢   
………………………………
   syms  phis2 phitheta thetas phis;              %定义符号函数:  
   phi2 = q2s * phis ...

yfc2 = inline(ysc2);改为 yfc2 = inline('ysc2');试试看
 楼主| 发表于 2007-10-8 10:22 | 显示全部楼层
谢谢

原帖由 花如月 于 2007-10-8 10:17 发表

yfc2 = inline(ysc2);改为 yfc2 = inline('ysc2');试试看

我把上面帖子改了一下,顺便贴出下面几句。
还是出错。

??? Error using ==> fzero at 317
FZERO cannot continue because user supplied inline object ==> yfc2
failed with the error below.
Error using ==> inline.subsref at 17
Too many inputs to inline function.
Error in ==> FaceGear at 61
        phisnum(k,m) = fzero(yfc2,0,options,thetaslist(k,m));            %#ok<AGROW>  %phis的取值矩阵

输入我数了一下,不多啊。除了几个syms定义的,其他其他都应该是double的。

[ 本帖最后由 eight 于 2007-10-8 11:10 编辑 ]
 楼主| 发表于 2007-10-8 10:46 | 显示全部楼层
inline函数里面只能有一个syms定义变量么?
输入两个就too many了?
我再看一下fzero函数

[ 本帖最后由 zqllc 于 2007-10-8 10:49 编辑 ]
发表于 2007-10-8 11:11 | 显示全部楼层
版面有很多 inline 函数的使用实例,请搜索一下
发表于 2007-10-8 11:38 | 显示全部楼层

回复 #3 zqllc 的帖子

一个一个调试吧,不懂就看help文档或者搜索版面。inline函数的错误已经排除了,又提示的是fzero的错误。自己找找吧,我们可不能一句一句帮你改程序呀:handshake
 楼主| 发表于 2007-10-8 16:29 | 显示全部楼层
原帖由 花如月 于 2007-10-8 11:38 发表
一个一个调试吧,不懂就看help文档或者搜索版面。inline函数的错误已经排除了,又提示的是fzero的错误。自己找找吧,我们可不能一句一句帮你改程序呀:handshake


收到,谢谢:)
发表于 2007-10-8 23:47 | 显示全部楼层
若还有问题,将参数及thetaslimits一并贴一下.------这一般是发贴的基本常识,也便于别人调试,提出建议.
 楼主| 发表于 2007-10-9 20:46 | 显示全部楼层
原帖由 xjzuo 于 2007-10-8 23:47 发表
若还有问题,将参数及thetaslimits一并贴一下.------这一般是发贴的基本常识,也便于别人调试,提出建议.

把问题简化了以下,出错还应该是inline函数的问题。
基本代码及错误信息是这样:

  1. clear;
  2. clc;
  3. syms phis thetas;
  4. thetaslist=randn(1,10);
  5. options = optimset('Display','off');
  6. ysc2 =-300.+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*...
  7.     cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas);
  8. yfc2 = inline('ysc2');
  9. for m = 1:10
  10.     ysc2=subs(ysc2,thetas,thetaslist(m));        %
  11.     phisnum(m) = fzero(yfc2,0,options,thetaslist(m));  %phis的取值矩阵
  12. end
复制代码


错误提示信息是:

  1. ??? Error using ==> fzero at 317
  2. FZERO cannot continue because user supplied inline object ==> yfc2
  3. failed with the error below.
  4. Error using ==> inline.subsref at 17
  5. Too many inputs to inline function.
  6. Error in ==> test_inline at 11
  7.     phisnum(m) = fzero(yfc2,0,options,thetaslist(m));  %phis的取值矩阵
复制代码
发表于 2007-10-9 21:09 | 显示全部楼层

回复 #9 zqllc 的帖子

完全属于输入不规范和语法错误导致的问题,下次用函数前还是仔细读读帮助文档和例子
  1. clear;
  2. clc;
  3. syms phis thetas;
  4. thetaslist=randn(1,10);
  5. options = optimset('Display','off');
  6. ysc='-300.+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas)';
  7. yfc2 = inline(ysc);
  8. for m = 1:10
  9.     ysc2=inline(subs(ysc,'thetas',thetaslist(m)));        %
  10.     phisnum(m) = fzero(yfc2,0,options,thetaslist(m)); %phis的取值矩阵
  11. end
  12. phisnum
复制代码
phisnum =

  Columns 1 through 9

   -0.0155   -0.1905   -0.3319    0.2570    0.3925    0.3669    0.2727    0.0071    0.4249

  Column 10

   -0.6003

[ 本帖最后由 花如月 于 2007-10-9 21:12 编辑 ]

评分

1

查看全部评分

 楼主| 发表于 2007-10-9 21:19 | 显示全部楼层
原帖由 花如月 于 2007-10-9 21:09 发表
完全属于输入不规范和语法错误导致的问题,下次用函数前还是仔细读读帮助文档和例子clear;
clc;
syms phis thetas;
thetaslist=randn(1,10);
options = optimset('Display','off');
ysc='-300.+46.98*sin( ...


收到,万分谢谢,以后还是要多多练习了。~~
 楼主| 发表于 2007-10-9 21:43 | 显示全部楼层
原帖由 花如月 于 2007-10-9 21:09 发表
完全属于输入不规范和语法错误导致的问题,下次用函数前还是仔细读读帮助文档和例子clear;
clc;
syms phis thetas;
thetaslist=randn(1,10);
options = optimset('Display','off');
ysc='-300.+46.98*sin( ...

:@L 实在基础比较弱了。
若改成下面这种形式,加了一个循环,

  1. clear;
  2. clc;
  3. syms phis thetas;
  4. thetaslist=randn(1,10);
  5. y2=randn(1,10)
  6. options = optimset('Display','off');
  7. for k = 1:10
  8.     ysc2 =y2(k)+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*
  9.     cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas);
  10.     yfc2 = inline(ysc2);
  11.     for m = 1:10
  12.         %  ysc2=subs(ysc2,thetas,thetaslist(m));        %
  13.         phisnum(m) = fzero(yfc2,0,options,thetaslist(m));  %phis的取值矩阵
  14.     end
  15. end
复制代码

似乎写成
  1. ysc2 ='y2(k)+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas)';
复制代码

是行不通的。提示变为[quoto]Error using ==> inline.subsref at 14
Not enough inputs to inline function.[/quote]
写成 yfc2 = inline('ysc2')更是不对,
似乎中间缺少某个转换函数。不过不知道怎么用。
发表于 2007-10-9 22:21 | 显示全部楼层

回复 #12 zqllc 的帖子

有必要弄个双循环么?你的目的到底是什么?是和第一次同样的错误,好好思考下吧
 楼主| 发表于 2007-10-9 22:36 | 显示全部楼层
原帖由 花如月 于 2007-10-9 22:21 发表
有必要弄个双循环么?你的目的到底是什么?是和第一次同样的错误,好好思考下吧

循环是有必要的。
嗯,错误是一样的。我再仔细看看。谢谢
 楼主| 发表于 2007-10-9 22:57 | 显示全部楼层
原帖由 花如月 于 2007-10-9 22:21 发表
有必要弄个双循环么?你的目的到底是什么?是和第一次同样的错误,好好思考下吧

嗯。改用@函数可以避免这个问题。感觉还是inline函数有局限。双重循环有问题。


  1. clear;
  2. clc;
  3. syms phis thetas;
  4. thetaslist=randn(1,10);
  5. y2=randn(1,10)
  6. options = optimset('Display','off');

  7. for k = 1:10
  8.      yfc2 = @(phis,thetas)(y2(k)+46.98*sin(.2000*phis)*(sin(phis+.6364e-1+thetas)-1.*thetas*cos(phis+.6364e-1+thetas))+234.9*cos(.2000*phis)/cos(phis+.6364e-1+thetas));
  9.     for m = 1:10
  10.         phisnum(m) = fzero(yfc2,0,options,thetaslist(m));  %phis的取值矩阵
  11.     end
  12. end
复制代码
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-23 23:34 , Processed in 0.068062 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表