How can I asign values to syms during a for loop
2 views (last 30 days)
Show older comments
Hi there,
I'm having a problem with the sysmbolics when I try to make a for loop to then plot the function for the different values of x.
The code is the following
syms w(x,y) W(x,y) g(y) yp z Ax
assume(y,'real')
assume(yp,'real')
% Ax = 300;
% Radius
R = 50;
q = 1/3;
k = 0.04;
sigmas = 1/3*(k*x+R)^2;
% c(x)
c = R^2*(1-q)/(2*sigmas);
% Tau squared
taus = 2/3*k*(k*x+R)*Ax;
w(x,y)=sqrt(c)*exp(-y^2/(2*sigmas));
g(z) = exp(-z^2/(2*taus))/sqrt(2*pi*taus);
g(y-yp);
f(x,y,yp,Ax) = g(y-yp)*w(x,yp);
% Some limits of integration
a = -1000;
b = 1000;
% Define W as the integral of f(x,yp) dy from a to b
% W(x,Ax,y) = int(f(x,y,yp,Ax), yp, a, b);
Ax = 1;
N=Ax:Ax:10*R;
%W(x,y) = zeros(length(N),1); % Pre-allocation. not sure if needed
for i = N
x = i-1;
W(x+i,y) = int(f(x,y,yp,Ax), yp, a, b);
i = i + 1;
end
So basically in the for loop the W function will be evaluated at x+deltaX and then the integral of the function on the right hand side will have x as value. Basically if we start at x=0 and deltaX=0.1, we will have W(0.1,y)=int(g(y-yp)*w(0,yp))
Any idea how could I make it work ?
0 Comments
Answers (1)
Brahmadev
on 27 Sep 2023
Hi Raul,
I understand that you would like to evaluate the integral "W" for each step of "deltaX". This can be accomplished without using a for loop. You can replace the code after defining the limits "a" and "b" with the following code:
Fnew = subs(f, Ax, 1); % Symbolic substitution of Ax as 1
Ax = 1; % Defining the value of Ax for proper evaluation of Ax:Ax:50*R
% Calculating the integral after substituting the value for x as an array
% with steps deltaX
Fnew2 = subs(Fnew, x, Ax:Ax:10*R);
W = int(Fnew2, yp, a, b);
Also, it is not necessary to define "Ax" and "yp" as a symbolic variables in the first line.
Hope this helps.
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!