how to plot from 0 to an output of a function using fplot

1 次查看(过去 30 天)
Hello, I am trying to plot something similar to the picture. I am using the code bellow tp plot, but there is a proplem with the interval here fplot(x,NcrN(n),[0 inter],'r','LineWidth',1.5). when I run the code, it is stating that there is an error using Error using fplot ''Too many functions''. Is there a way to fix this issue without substituting the value of inter manually?
syms z L EI kd N n c d x
Ld=10;
Ncr1 =(2.4674*EI*(2*n - 1)^2)/L^2;
Ncr2 =-(0.0205*L^3*kd*(39.4784*n + 2*cos(6.2832*n) - 39.4784*n^2 - 7.8696))/(d*(2*n - 1)^4);
n1=1:10;
Ncr22=subs(Ncr1,n,n1)+subs(Ncr2,n,n1);
for n=1:n1
%Ncr=Ncr22(n);
NcrN=subs(Ncr22,[L EI d L kd],[1 1/x^2/4 1/Ld 1 x^2]);
int=solve(NcrN(n+1)-NcrN(n)==0,x);
inter=int(real(int)>0&imag(int)==0);
fplot(x,NcrN(n),[0 inter],'r','LineWidth',1.5)
end
fplot(NcrN.','--');
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-4-5
1 - You have not defined what Ld and x are, so I have assumed them to be symbolic variables.
2 - use n1 as the loop index instead of 1:n1, as n1 is an array and not a scalar.
3 - Ncr and y are not used in the code. Bring Ncr22 and NcrN out of the loop as they are not varying with the loop.
4 - Do not use "int" as a variable name as it is a function in MATLAB.
5 - There is no guarantee that any of the root obtained, assigned to int0, will be real. The roots obtained are in terms of Ld ,which is a symbolic variable. If Ld = -1, then all roots will be imaginary. Additionally, inter can be an array as well, which you can not put as limits to plot the function in.
It's not clear which function from the code is supposed to be the red curve in the image above. And the red curve doesn't cross the x-axis in the image, so it's not clear what your title is supposed to be.
syms z L EI kd N n c d x Ld
Ncr1 =(2.4674*EI*(2*n - 1)^2)/L^2;
Ncr2 =-(0.0205*L^3*kd*(39.4784*n + 2*cos(6.2832*n) - 39.4784*n^2 - 7.8696))/(d*(2*n - 1)^4);
n1=1:10;
Ncr22=subs(Ncr1,n,n1)+subs(Ncr2,n,n1);
NcrN=subs(Ncr22,[L EI d L kd],[1 1/x^2/4 1/Ld 1 x^2]);
for n=1%n1
%Ncr=Ncr22(n)
int0=solve(NcrN(n+1)-NcrN(n)==0,x)
%inter=int0(real(int0)>0&imag(int0)==0);
%y=subs(NcrN(n),x,inter)
%fplot(x,NcrN(n),[0 inter],'r','LineWidth',1.5)
end
int0 = 
%fplot(NcrN.','--');
SAM
SAM 2023-4-5
I am actually not looking for this, sorry I deleted some parts of the code cuz it's really long. The only thing I need to know is how to solve the problem of too many functions; it is basically caused by the fact that I am using the interval from [0 inter]. So I just wanna know how to make matlab atomatically use the value of inter (which is 1.4947 for n=1 ) as a numerical value.
syms z L EI kd N n c d x
Ld=10;
Ncr1 =(2.4674*EI*(2*n - 1)^2)/L^2;
Ncr2 =-(0.0205*L^3*kd*(39.4784*n + 2*cos(6.2832*n) - 39.4784*n^2 - 7.8696))/(d*(2*n - 1)^4);
n1=1:10;
Ncr22=subs(Ncr1,n,n1)+subs(Ncr2,n,n1);
for n=1:n1
%Ncr=Ncr22(n);
NcrN=subs(Ncr22,[L EI d L kd],[1 1/x^2/4 1/Ld 1 x^2]);
int=solve(NcrN(n+1)-NcrN(n)==0,x);
inter=int(real(int)>0&imag(int)==0);
fplot(x,NcrN(n),[0 inter],'r','LineWidth',1.5)
end
Error using fplot
Too many functions.
fplot(NcrN.','--');

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-4-5
编辑:Dyuman Joshi 2023-4-5
Convert the solution obtained by solve() to double.
syms z L EI kd N n c d x
Ld=10;
Ncr1=(2.4674*EI*(2*n - 1)^2)/L^2;
Ncr2=-(0.0205*L^3*kd*(39.4784*n + 2*cos(6.2832*n) - 39.4784*n^2 - 7.8696))/(d*(2*n - 1)^4);
n1=1:10;
Ncr22=subs(Ncr1,n,n1)+subs(Ncr2,n,n1);
NcrN=subs(Ncr22,[L EI d L kd],[1 1/x^2/4 1/Ld 1 x^2]);
%1 to second last as the index to NcrN is n+1
for n=n1(1:end-1)
int0=solve(NcrN(n+1)-NcrN(n)==0,x);
int0=double(int0);
inter=int0(real(int0)>0&imag(int0)==0);
%Different color for each plot to easily distinguish
fplot(x,NcrN(n),[0 inter],'Color',rand(1,3),'LineWidth',1.5)
hold on
end
%Change the xlim to see the variation of curves properly
xlim([0 0.5])
figure
fplot(NcrN.','--');

更多回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by