Code taking too long to execute

조회 수: 2 (최근 30일)
Osama Anwar
Osama Anwar 2020년 12월 12일
댓글: Osama Anwar 2020년 12월 12일
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

채택된 답변

Ive J
Ive J 2020년 12월 12일
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
Osama Anwar
Osama Anwar 2020년 12월 12일
Thanks man it resolved my problem.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by