0

Here is my Matlab code

syms x(a) a
x(a) = a ;
D = [[1], [2.6], [3.0], [3.3], [3.5], [3.9], [4.0], [4.2], [5.7]];

syms g(z) z x(a) D(a)
g(z) = symsum(x(a).*D(a).*exp(-z.*x(a)), a, 1, 9) + 75.*x(10).*exp(-z.*x(10)) ;
h = diff(g)  ;
z(1)=0.6 ;

for m=1:1000
    z(m+1) = z(m) - g(z(m))/h(z(m));
    if( abs( g(z(m+1)) - g(z(m)) ) < 0.00001 )
        disp(double(z(m+1)));
        break;
    end
end

When trying to run it I keep getting the error: Conversion to logical from sym is not possible.

Error in errcode (line 12) if( abs( g(z(m+1)) - g(z(m)) ) < 0.00001 )

However, I thought in my following line the use of double() would fix this issue and return a float value. Does anyone have any advice?

Viv4660
  • 101
  • Split your complex condition in smaller parts to determine which operation exactly causes the error. – MrSmith42 Mar 30 '22 at 12:23
  • @MrSmith42 Do you mean split line 12 into smaller parts? If so, I'm not exactly sure how I can make |a - b| < k smaller? – Viv4660 Mar 30 '22 at 12:26
  • @MrSmith42 I just tried putting line 12 over several lines (using ellipses) and it highlighted the line with the abs() function as the error. So I think taking the absolute value of `g(z(m+1)) - g(z(m))` may be the issue. – Viv4660 Mar 30 '22 at 12:51
  • 1
    You only convert to double to display, why would that change anything? Why would that change anything that happens _before_? In any case, this all seems very numerical to me, why use `syms` at all? – Ander Biguri Mar 30 '22 at 12:55
  • @AnderBiguri Good point. Well I can see how I can easily eliminate my first use of `syms` (line 1) by defining `x` similarly to how I defined `D`. However (and perhaps most importantly), I'm not sure how I'd eliminate the use of `syms` when defining `g(z)`, especially since it involves a summation. – Viv4660 Mar 30 '22 at 13:12
  • 1
    I am still unsure if you know what you are doing here. It has a sumation, which can be numerically done with the symbol `+` ot just `sum`. You ultimately want to evaluate everything numerically, so I don't think you need symbolic math. Are you trying to make `g(z)` a function, in the programming sense? This code is all over the place anyway. How do you want to convert to numbers if you never define `a` as a number? `abs( g(z(m+1)) - g(z(m))` will be something that has `a` as a symbol right? so you can never know if its <0.00001 unless `a` has a numerical value – Ander Biguri Mar 30 '22 at 13:41
  • In short, this all looks like you trying to tackle a complex problem without understanding the basics of the tools you are using, there are too many things wrong with the code to the point that I don't even know where to start helping you! I suggest having a look at the docs and what each thing do, and describing your goal with text, not just code. – Ander Biguri Mar 30 '22 at 13:43
  • @AnderBiguri It can be done with the symbol `+` yes, but that means I will be specifying nine separate terms, which certainly isn't practical on larger data sets which you often come across when using numerical methods. As for the `sum` function, I'm pretty sure it doesn't let you specify a summation index which doesn't make it appropriate for my problem. Lastly, if you familiarise yourself with the `symsum()` function, you'll notice that `a` is the summation index and so when the sum is evaluated it disappears. So, your point that `a` doesn't have a numerical value is incorrect. – Viv4660 Mar 30 '22 at 13:49
  • Fair point about `a`, but all the rest still stands. What is `x(1)`? You don't need a "summation index". `sum(D)` will sum numerical values in an array `D`. `sum(D(1:2:end))` will sum every 2. I am still 100% certain you don't need `syms` if you want a number in the end. But for now, you can not have a number because `x(a)` is not numerically defined. – Ander Biguri Mar 30 '22 at 13:57
  • @AnderBiguri The first `syms` (line 1) specifies that `x(a)=a` so `x(1)=1`. But doesn't using `sum(D)` still mean that I'd need array `D` to contain each term of the sum? I don't see how this would be practical for larger data sets. – Viv4660 Mar 30 '22 at 14:05
  • symbolic mathematics is 4 orders of magnitude slower to run in a computer than numerical mathematics, so if you are worried about large arrays, you seem to be choosing wrong. What is large for you though? – Ander Biguri Mar 30 '22 at 14:06

1 Answers1

1

A numerical version of your code:

D = [1, 2.6, 3.0, 3.3, 3.5, 3.9, 4.0, 4.2, 5.7];
x=1:length(D)+1; 
g = @(z)sum(x(1:end-2).*D(1:end-1).*exp(-z.*x(1:end-2))) + 75.*x(end).*exp(-z.*x(end)) ;
h = @(z)- 75*exp(-z*x(end))*x(end)^2 +sum(-D(1:end-1).*exp(-z.*x(1:end-2)).*x(1:end-2).^2);
z(1)=0.6;
for m=1:1000
    z(m+1) = z(m) - g(z(m))/h(z(m));
    if( abs( g(z(m+1)) - g(z(m)) ) < 0.00001 )
        disp(double(z(m+1)));
        break;
    end
end

Please double check the equations for g and h, I did not put too much effort on the correctness of the equation, there may have a bug.

This runs in 11.371ms in my PC. In comparison, the line h = diff(g); in your symbolic version takes 34.027ms

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120