symbolic variable and assignment operator

2 次查看(过去 30 天)
syms x
syms t
b0=0.05
a3=0.1
b3=0.3;
A=(2*b3-1)/(2*a3-1)
B=(1/2)*b0*((4*a3*b3-1)/2*a3-1);
U=zeros(1,2,'sym');
V=zeros(1,2,'sym');
A=zeros(1,2,'sym');
B=zeros(1,2,'sym');
C=zeros(1,2,'sym');
D=zeros(1,2,'sym');
series1(x,t)=sym(zeros(1,1));
series2(x,t)=sym(zeros(1,1));
U(1)=0.05*(1-tanh(B*(20*(x-0.5))));
V(1)=b0*(A-tanh(B*(20*(x-0.5))))
Unable to perform assignment because the left and right sides have a different number of elements.
Error in sym/privsubsasgn (line 1168)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in indexing (line 999)
C = privsubsasgn(L,R,inds{:});
  1 个评论
Shantanu Dixit
Shantanu Dixit 2024-8-21
Hi Yogeshwari, the error occurs because you're trying to assign a vector (1x2) to a single element of the symbolic array U and V.
U(1) = 0.05 * (1 - tanh(B * (20 * (x - 0.5))));
V(1)=b0*(A-tanh(B*(20*(x-0.5))));
%% results in a 1x2 vector on the right side, which cannot be assigned to a single element on the left
Also double-check your implementation as 'B' is initially defined as a symbolic variable and is later changed to a symbolic vector making the initial declaration redundant.
B=(1/2)*b0*((4*a3*b3-1)/2*a3-1);
B=zeros(1,2,'sym');

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-8-21
B=zeros(1,2,'sym');
B will be a symbolic vector of size 1 x 2
U(1)=0.05*(1-tanh(B*(20*(x-0.5))));
On the right hand side, all of the symbolic vector B is used, so the right hand side will be a 1 x 2 result. But you are attempting to assign that 1 x 2 result into a single location, U(1)
  1 个评论
Steven Lord
Steven Lord 2024-8-21
If you'd eliminated the line of code that overwrites the scalar you'd assigned to B first with the symbolic vector, it would give you an answer. I'll leave it to you to determine if that's the answer you expected.
syms x
syms t
b0=0.05
b0 = 0.0500
a3=0.1
a3 = 0.1000
b3=0.3;
% These assign scalars to A and B
A=(2*b3-1)/(2*a3-1)
A = 0.5000
B=(1/2)*b0*((4*a3*b3-1)/2*a3-1)
B = -0.0261
U=zeros(1,2,'sym');
V=zeros(1,2,'sym');
% Commenting these two lines out (so A and B aren't overwritten) ...
% A=zeros(1,2,'sym');
% B=zeros(1,2,'sym');
C=zeros(1,2,'sym');
D=zeros(1,2,'sym');
series1(x,t)=sym(zeros(1,1));
series2(x,t)=sym(zeros(1,1));
% ... allows these two lines to work
U(1)=0.05*(1-tanh(B*(20*(x-0.5))))
U = 
V(1)=b0*(A-tanh(B*(20*(x-0.5))))
V = 

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by