MATLAB Answers

Error in plotting an equation given y data sets (y vs x)

33 views (last 30 days)
lvenG
lvenG on 20 Sep 2021
Commented: Walter Roberson on 20 Sep 2021
Hello. I am having trouble plotting a curve y vs x, given y data set from 1 to 30 with an interval of 0.5, through a given equation of
y=52.4*T*((1./x)+(C./(x)^2)); where T and C are constants
The error I am getting is because of the 'sym' that I cannot seem to understand. Please see my code below. Hoping for some help and assistance here. Thank you so much.
syms y x
T=450;
sigma=58.7;
lambda=32.9;
sigma_n=32.3;
lambda_n=7.5;
A=8.314*lambda+sigma/2;
B=32.4*lambda_n+sigma_n/2;
C=sqrt(A*B)/(lambda*lambda_n);
y=[1:0.5:30];
n=(30-1)/0.5;
for i=1:(n+1)
y(i)=52.4*T*((1./x(i))+(C./(x(i))^2));
end
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
plot(x,y);
  1 Comment
KSSV
KSSV on 20 Sep 2021
Fist solve the equation for x and then substitue y and then plot.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2021
Your code wants to calculate the formula for each y value, for all possible x values. But to do that, you have to make the symbolic variable x into a vector of symbolic variables the same size as your vector y.
T=450;
sigma=58.7;
lambda=32.9;
sigma_n=32.3;
lambda_n=7.5;
A=8.314*lambda+sigma/2;
B=32.4*lambda_n+sigma_n/2;
C=sqrt(A*B)/(lambda*lambda_n);
y = sym([1:0.5:30]).';
x = sym('x', size(y));
n=(30-1)/0.5;
for i=1:(n+1)
y(i)=52.4*T*((1./x(i))+(C./(x(i))^2));
end
y
y = 
  4 Comments
Walter Roberson
Walter Roberson on 20 Sep 2021
In this particular case there is a closed form formula that you can use to calculate the general form, after which you can put in specific y values, instead of having to loop finding the values one by one.
T=450;
sigma=58.7;
lambda=32.9;
sigma_n=32.3;
lambda_n=7.5;
A=8.314*lambda+sigma/2;
B=32.4*lambda_n+sigma_n/2;
C=sqrt(A*B)/(lambda*lambda_n);
syms x Y positive
eqn = Y == 52.4*T*((1./x)+(C./(x)^2));
solX = solve(eqn, x)
solX = 
y = [1:0.5:30].';
X = double(subs(solX, Y, y))
X = 59×1
1.0e+04 * 2.3581 1.5721 1.1791 0.9433 0.7861 0.6738 0.5896 0.5241 0.4717 0.4288

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!