Code taking too long to execute

17 vues (au cours des 30 derniers jours)
Osama Anwar
Osama Anwar le 12 Déc 2020
Commenté : Osama Anwar le 12 Déc 2020
I'm ploting a 3d graph of displacement of single degree of freedom system against a loading of sin wave.
First I inputed the function. It has its value in terms of a and b. I calculated a and b as initial conditions were given for the function. The final equation I get is only in form of variables x(time) and y(beta).
I have to plot a 3d graph against them. For this I have to mesh x and y and equate z to my displacement function.
But the program is taking too long to process the part where I equate z to the displacement function (i.e. z=double(G(xx,yy));). I have almost zero background in programming. What changes can I make so that matlab takes less time to execute this part?
Here is my code
clear
clc
syms w wd x a b zai wbar eq1 eq2 u0 v0 y z xx yy
zai=0.05;
po=1000;
k=40609481.90;
m=12000;
w=sqrt(k/m);
wbar=y*w;
wd=w*sqrt(1-zai^2);
F(x,y)=exp(-zai*w*x)*(a*cos(wd*x)+b*sin(wd*x))+po/k*1/((1-y^2)^2+(2*zai*y)^2)*((1-y^2)*sin(y*w*x)-(2*zai*y)*cos(y*w*x));
F(0,y);
derivF(x,y)=diff(F(x,y),'x');
%derivF(0,y);
eq1=F(0,y)==0;
eq2=derivF(0,y)==0;
sol=solve([eq1,eq2],[a,b]);
%sol.a;
%sol.b;
avalue=sol.a;
bvalue=sol.b;
G(x,y)=subs(F(x,y),[a,b],[avalue,bvalue]);
%G(x,y);
%double(G(0.003,0.05));
x=0:0.005:2.5;
y=0:0.005:2.5;
[xx,yy]=meshgrid(x,y);
z=double(G(xx,yy));
figure
surf(xx,yy,z)
shading interp

Réponse acceptée

Ive J
Ive J le 12 Déc 2020
Replace this line
z = double(G(xx, yy));
with these
G = matlabFunction(G); % generate a function handle from your symbolic G
z = G(xx, yy);
  1 commentaire
Osama Anwar
Osama Anwar le 12 Déc 2020
Thanks man it resolved my problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Styling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by