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

조회 수: 2 (최근 30일)
lvenG
lvenG 2021년 9월 20일
댓글: Walter Roberson 2021년 9월 20일
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
KSSV
KSSV 2021년 9월 20일
Fist solve the equation for x and then substitue y and then plot.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 20일
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
lvenG
lvenG 2021년 9월 20일
Yes, the data set given is for y... so x shall be calculated implicitly... Now I know I should put solve functions for operations of this kind. Really thankful for your help. :)
Walter Roberson
Walter Roberson 2021년 9월 20일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by