symbolic function to numeric value
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have used this code to solve second order different equaion.Now I want to get all the value of j1 from 0 to 30. the answer comes in terms of T,how do i put numerical value of t to obtianed equation amd store those numerical values?
syms j1(t)
Dj1=diff(j1);
ode = (20*diff(j1,t,2)+20*diff(j1,t)+100*j1)==f1;
cond1=j1(0)==0;
cond2=Dj1(0) == 0;
conds=[cond1,cond2];
j1Sol(t)=dsolve(ode,conds);
j1Sol=simplify(j1Sol);
0 commentaires
Réponses (1)
Star Strider
le 3 Jan 2021
First, ‘f1’ has to appear somewhere before it is used.
With that correction, try this:
syms j1(t) f1
Dj1=diff(j1);
ode = (20*diff(j1,t,2)+20*diff(j1,t)+100*j1)==f1;
cond1=j1(0)==0;
cond2=Dj1(0) == 0;
conds=[cond1,cond2];
j1Sol(t)=dsolve(ode,conds);
j1Sol=simplify(j1Sol, 'Steps',100);
j1Sol_fcn = matlabFunction(j1Sol, 'Vars',{t, f1});
f1 = 42;
t = linspace(0, 42, 420);
figure
plot(t, j1Sol_fcn(t,f1))
grid
4 commentaires
Star Strider
le 3 Jan 2021
After a few edits, this appears to work:
a1=1;
a2=2;
i=0;
tv=0:30;
for k = 1:numel(tv)
t = tv(k);
i=i+1;
if t<10
f1=0;
f2=0;
end
if t>=10&&t<11
f1=a1*(1-cos(pi*t));
f2=a2*(1-cos(pi*t));
end
if t>=11&& t<20
f1=2*a1;
f2=2*a2;
end
if t>=20 && t<21
f1=a1*(1+cos(pi*t));
f2=a2*(1+cos(pi*t));
end
if t>=21
f1=0;
f2=0;
end
f1_list(:,i)=[f1];
f2_list(:,i)=[f2];
%solving impedence
syms j(t1) f t1
Dj=diff(j);
ode = (20*diff(j,t1,2)+20*diff(j,t1)+100*j)==f;
cond1=j(0)==0;
cond2=Dj(0) == 0;
conds=[cond1,cond2];
jSol(t1)=dsolve(ode,conds);
jSol=simplify(jSol, 'Steps',100);
j1Sol_fcn = matlabFunction(jSol, 'Vars',{t1, f});
t1=[t];
f=f1;
j1=j1Sol_fcn(t1,f);
j1_list(:,k)=j1;
f=f2;
j2=j1Sol_fcn(t1,f);
j2_list(:,k)=j2;
end
The code is not very efficient, however I am not certain what you are doing, so the only changes I made to it were those necessary to get it to run.
Consider possibly putting the ODE code before the loop, and using the 'Vars' name-value pair to add the appropriate arguments to it. Then substitute the appropriate arguments to ‘j1Sol_fcn’ in the loop to get ‘j1’ rather than creating ‘j1Sol_fcn’ in each iteration of the loop.
.
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!