Azzera filtri
Azzera filtri

symbolic function to numeric value

4 visualizzazioni (ultimi 30 giorni)
Madhav rijal
Madhav rijal il 3 Gen 2021
Commentato: Star Strider il 3 Gen 2021
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);

Risposte (1)

Star Strider
Star Strider il 3 Gen 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
See the documentation on matlabFunction for details on how to use it.
  4 Commenti
Madhav rijal
Madhav rijal il 3 Gen 2021
clear
clc
a1=1;
a2=2;
i=0;
for t=0:30
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
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);
jSol_fcn = matlabFunction(jSol, 'Vars',{t1, f1});
t1=[t];
f=f1;
j1=j1Sol_fcn(t1,f);
j1_list(:,i)=j1;
f=f2;
j2=j1Sol_fcn(t1,f);
j2_list(:,i)=j2;
end
this is what i am trying to do
Error using sym/matlabFunction>getOptions (line 656)
The value of 'Vars' is invalid. 'Vars' value must be a character vector, a 1-dimensional cell array of character vectors, a 1-dimensional cell array of symbolic variables or arrays of symbolic
variables, or an array of symbolic variables.
Error in sym/matlabFunction (line 154)
opts = getOptions(args);
Error in force_control (line 44)
jSol_fcn = matlabFunction(jSol, 'Vars',{t1, f1});
Got this
Star Strider
Star Strider il 3 Gen 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.
.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by