Error in plotting an equation given y data sets (y vs x)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
lvenG
am 20 Sep. 2021
Kommentiert: Walter Roberson
am 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
plot(x,y);
1 Kommentar
Akzeptierte Antwort
Walter Roberson
am 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
4 Kommentare
Walter Roberson
am 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)
y = [1:0.5:30].';
X = double(subs(solX, Y, y))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!