evaluate symbolic expression in MATLAB


evaluate symbolic expression in MATLAB



I am trying to evaluate a function which is an infinite cosine series at some input values.



EDIT: Posting an image to describe what the infinite series looks like
enter image description here



I wrote the following code to describe it in MATLAB.


function func = cosfun_hat(a,i)
syms m x;

assume(m,'integer');
assumeAlso(m > 0);

sum(x) = sqrt(1-a^2)*symsum(sqrt(2)*a^m*cos(i*sym(pi)*x*2^m+1),m,0,Inf);
func(x) = sum(x);
end



I want to evaluate the returned 'function' func to get numerical values for some input range say x_in = 0:0.001:1.


func


x_in = 0:0.001:1


%Trying to evaluate func at x = 2
%In the command window I write
func = cosfun_hat(0.5,2);
func(2)



which returns the symbolic expression:


(2^(1/2)*3^(1/2)*sum((1/2)^m*(exp(- pi*exp(m*log(2))*4*i - i)/2 + exp(pi*exp(m*log(2))*4*i + i)/2), m == 0..Inf))/2



I tried using subs to evaluate the expression:


subs


%In the command window
syms y;
w(y) = func(y);
y = 2;
subs(w);



But that returns the same symbolic expression. I am quite new to symbolic MATLAB.



Thanks!



EDIT Based on the comment by @NickyMattsson I tried


vpa(func(2))



which returns the numerical value of the expression.
However,
vpa(func(0.1)) returns a symbolic expression:


vpa(func(0.1))


ans =

1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)



The same problem with using double(func(0.1)), double doesn't return anything and is stuck.


double(func(0.1))


double



This question has not received enough attention.





Does vpa(func(2)) solve your problem?
– Nicky Mattsson
Jun 27 at 18:12


vpa(func(2))





@NickyMattsson Thanks for your comment. vpa(func(2)) returns numerical value.However if I use vpa(func(0.1)), MATLAB returns 1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)...
– Vizag
Jun 27 at 18:22



vpa(func(2))


vpa(func(0.1))


1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)




2 Answers
2



Figured out a way to do it without using symbolic MATLAB.


function func = cosfun_hat(a,i,x)
%syms m;
% assume(m,'integer');
% assumeAlso(m > 0);
%
m = 0;
sum = zeros(1,length(x));
sum2 = Inf(1,length(x));
while max(sum2-sum) > 1e-16
disp(m);
sum2 = sum;
sum = sum + sqrt(1-a^2)*sqrt(2)*a^m*cos(i*pi*x*2^(m+1));
m = m+1;

end
func = sum;
end



The sum converges inside 100 iterations.



Now if I do,


%In command window
x_in = -2:0.001:2;
f = cosfun_hat(0.6,2,x_in);
plot(x_in,f);



I get the plot:



enter image description here



Thanks everyone for your help!



Use this command


double(func(2))





double(func(2)) returns a numerical value.double(func(0.1)) doesn't return anything. As I have said in my question, I need to evaluate on a range of values x_in = 0:0.01:1, so I need to be able to get result for fractional values as well.
– Vizag
Jun 27 at 18:32


double(func(2))


double(func(0.1))


x_in = 0:0.01:1





func(0.1) contains complex number and can be converted to numerical value.
– PyMatFlow
Jun 27 at 18:37





how can there be a complex part? Each term in the infinite series is real valued.
– Vizag
Jun 27 at 18:39





Complex number can be created in many ways. When i take a look at result of func(0.1) , I see complex symbol in the output. I do not generate it. MATLAB generates the result.
– PyMatFlow
Jun 28 at 6:36





No. the 'i' in the result is the one I gave as input and not iota. Its symbolically represented. Change cosfun_hat(a,i) to cosfun_hat(a,k) and you'll see the symbol 'k' in the result obtained
– Vizag
Jun 28 at 6:38


cosfun_hat(a,i)


cosfun_hat(a,k)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift