Using a range as an input value in symbolic equations
21 views (last 30 days)
Show older comments
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:
- solve the equation (eqn) for x and return only positive x (X)
- calculate k and c with a calculated X and given variables
- 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.
0 Comments
Accepted Answer
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
% 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
k = beta*alpha^2./X.^2
c = 2.*A.*sqrt(k.*beta)
figure();
fplot(k,A)
hold on
fplot(c,A)
More Answers (0)
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!