質問

I'm having a problem with a user defined function I am constructing here. What I'm trying to do is to substitute a value into a symbolic function and then use that numerical answer for various purposes. Specifically here:

x = xo;
subst = subs(f,x);
while((n>i) && (subst > eps))

Running my program, I get the following error:

>> sym_newtonRaphson(f,fdiff,1,1e-8,10)
Conversion to logical from sym is not possible.

Error in sym_newtonRaphson (line 8)

I've tried using double(subs(f,x)) to no avail. I seem to be getting a totally different error relating to MuPAD (DOUBLE cannot convert the input expression into a double array.)

The following is the whole program:

function [output] = sym_newtonRaphson(f,fdiff,xo,eps,n)

i = 0;
%initial iteration
x = xo;
subst = subs(f,x);

  while((n>i) && (subst > eps))
     x = x - (subs(f,x))/fdiff;
     i = i+1;
     subst = subs(f,x);
     %fprintf('%f\t%f\t%f\t%f\t%f\t%f',i,alpha,f(
  end
  output = x;
end

I'd appreciate some pointers as to what I'm doing wrong; all the best.

役に立ちましたか?

解決

What you're trying to do with the while expression is equivalent to logical(f), where f is a symbolic function (rather than a symbolic value). logical(sym('exp(1)') > 0) is fine, but logical(sym('exp(f)') > 0) is generally not going to be (see assume). Matlab has no way of casting a symbolic variable to a logical (true and false) one. It tries to do this because the short circuit AND operator, &&, isn't supported for symbolic variables. For example

a = 1.5;
syms x;

% All of these will not generate errors
y1 = x > 1;
y2 = x > 1 & x < 2;
y3 = x > 1 & x < 2;
y4 = x > 1 & a < 2;
y5 = x > 1 & a > 2;

% These will result in errors
y2 = x > 1 && x < 2;
y3 = x > 1 && x < 2;
y4 = x > 1 && a < 2;
y5 = x > 1 && a > 2;

You should print out subst and make sure that it is a symbolic value or a function that does not include any variables (if argnames(subst) returns an empty symbolic matrix then you should be okay). The fact that you get the second error when you call double seems to imply that subst is actually an expression that still includes unknown variables. If this is the case, then you'll either need to substitute in other variables or use assumptions (see here) in order to do logical comparisons as you're doing.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top