Using a range as an input value in symbolic equations

26 views (last 30 days)
Hello,
I would like to use a range of value, instead of a specific value, as one of inputs in a symbolic equation, but getting errors, and hope I can get help.
Here is a script I wrote:
syms A omega x positive
% Parameters
alpha = 6*pi;
beta = 25;
omega = 0.1;
A = [0 0.5];
% A = 0.1;
eqn = omega == sqrt((1 + (2*A.*x).^2) ./ ((1-x^2)^2 + (2*A.*x).^2)) ;
X = vpa(solve(eqn,x));
k = beta * alpha^2 / X.^2;
c = 2 .* A .* sqrt(k.*beta)
figure();
fplot(k,A)
hold on
fplot(c,A)
What I am trying to do is:
  1. solve the equation (eqn) for x and return only positive x (X)
  2. calculate k and c with a calculated X and given variables
  3. plot k and c value with respect to A
It returns a correct k and c values when I set A a single value, but when I set this a range of value I get the following error:
Error using .*
Array sizes must match.
When I put A below " X = vpa()" line, it plots right k values, but not c. (it should print around c= 28 when A = 0.1, but gives me around c= 140 in the graph, and now c has a 1x2 system)
I would appreicate your help.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 25 Apr 2023
Edited: Dyuman Joshi on 25 Apr 2023
You get the error because A is 1x2 and x is 1x1 and you can not multiply 1x2 to 1x1. Now you would think of changing the order, but in that case, the system will have 2 equations with 1 variable, which is not consistent.
You can either
1 - Use a loop, or,
2 - Define x as 1x2 sym, and proceed further
syms x positive
whos x
Name Size Bytes Class Attributes x 1x1 8 sym
% Parameters
alpha = 6*pi;
beta = 25;
omega = 0.1;
A = [0 0.5];
% A = 0.1;
%Loop
%Assuming you are sure that only 1 positive root exists
%for each element of A
for k=1:numel(A)
X(k) = vpasolve(omega == sqrt((1 + (2*A(k)*x).^2)./((1-x^2)^2 + (2*A(k)*x).^2)));
end
X
X = 
k = beta*alpha^2./X.^2
k = 
c = 2.*A.*sqrt(k.*beta)
c = 
figure();
fplot(k,A)
hold on
fplot(c,A)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!