Hello everybody
i just have this code:
syms w1 w2 w3 w4 v1 v2 x1 x2 yd
n1=w1*x1+w2*x2;
o1=1/(1+exp(-n1));
o1_der=o1*(1-o1);
%%
n2=w3*x1+w4*x2;
o2=1/(1+exp(-n2));
o2_der=o2*(1-o2);
%%
n3=v1*o1+v2*o2;
o3=(1/(1+exp(-n3)));
o3_der=o3*(1-o3);
e1=(o3-yd);
%%
A1=((o1^2+o2^2)*o3_der^2 +(x1^2+x2^2)*(v1^2)*o1_der^2*o3_der^2+(x1^2+x2^2)*(v2^2)*(o2_der^2)*o3_der^2);
%%
aa=e1*A1;
aw1=diff(aa,w1);
I need to take (aw1) which is derived (aa) respect to (w1) then use it in b.m file for example.. (aw1 is symbolic i mean all values inside are varibals).
The error below occure when i save aw1 as a data and load it in another file and run
(Inputs must be floats, namely single or double.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in simple (line 44)
[T1,cox]=ode15s(@(t,y1) Costatesimple(t,y1,u,x1,x2,yd,x1t,e1,w1,w2,w3,w4,v1,v2),tspan2,[0 0 0 0 0 0 0],options);
any help please?
however, it works when i copy all values insied aw1 and paste it in b.m file..
due to the value of aw1 is too long so, i need way to move it to another file without copy and paste
i would you to help in this issue please.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 29 Mar 2020
When saving aw1 in a.m you need to save all other symbolic variables too
save('filename.mat', 'aw1', 'w1', 'w2', 'w3', 'w4', 'v1', 'v2', 'x1', 'x2', 'yd' );
and then load the file in b.m
load('filename.mat');
Alternatively, you can also try to just save aw1
save('filename.mat', 'aw1')
and then in b.m write
load('filename.mat');
syms w1 w2 w3 w4 v1 v2 x1 x2 yd
For a bit detailed discussion on this topic, refer to Walter's answer and my comments here: https://www.mathworks.com/matlabcentral/answers/513625-how-to-save-a-symbolic-equation-in-a-txt-file#answer_422592

10 Comments

Ali Najem
Ali Najem on 29 Mar 2020
thank you so much ameer for your time to solve my issue it works well
but when i but it in my file.m which is function file i got the same error
Inputs must be floats, namely single or double.
Is that any way to just move or transfer my answes as it is i mean i want some method to do so
when i run my file.m it is already will take the aw1 from symbolic file
thanks in advance
Ameer Hamza
Ameer Hamza on 29 Mar 2020
Can you share your file. I will make it easy for me to understand the issue.
function dy1dt = Costatesimple(t,y1,u,x1,x2,yd,x1t,e1,w11,w21,w31,w41,v11,v21)
%%
u=u(floor(t+1));
e1=interp1(x1t,e1,t);
w1 = interp1(x1t,w11,t);
w2 = interp1(x1t,w21,t);
w3 = interp1(x1t,w31,t);
w4 = interp1(x1t,w41,t);
v1 = interp1(x1t,v11,t);
v2 = interp1(x1t,v21,t);
n1 = w1*x1 + w2*x2;
o1 = 1/(1+exp(-n1));
o1_der = o1 *(1-o1);
n2 = w3*x1 + w4*x2;
o2 = 1/(1+exp(-n2));
o2_der = o2 *(1-o2);
n3 = v1* o1 + v2*o2;
o3 = yd*(1/(1+exp(-n3)));
o3_der = o3 *(1-o3);
%%
A1=((o1^2 + o2^2)*o3_der^2 + (x1^2+x2^2)* v1^2* o1_der^2* o3_der^2+ (x1^2+x2^2)* v2^2* o2_der^2* o3_der^2);
A2= x1*o1_der*o3_der;
A3= x2*o1_der*o3_der;
A4= x1*o2_der*o3_der;
A5= x2*o2_der*o3_der;
A6= o1*o3_der;
A7= o2*o3_der;
%%
dy1dt=[-(-u*A1*y1(1)-u*(A2*y1(2)*v1+ A3*y1(3)*v1+ A4*y1(4)*v2+ A5*y1(5)*v2+ A6*y1(6)+ A7*y1(7)));
-(-u*y1(1)*(aw1)];
end
as you can see this function file that i want to use (aw1) here hence, aw1 taken from symblic file
by the way of course I have a main file which is call this function
thanks agian
Ameer Hamza
Ameer Hamza on 29 Mar 2020
How do you define aw1 in this file. There is no load() function.
Ali Najem
Ali Najem on 29 Mar 2020
i used the same way that you told me save and load but doesn't work in this file
i just need to take aw1 from symblic file and use it in this file
Ameer Hamza
Ameer Hamza on 29 Mar 2020
Can you show some input values so that I can call this function. Also note that if you are doint to use it with ode15s, then dy1dt should be numeric. ode15s does not work with symbolic variables.
Ali Najem
Ali Najem on 29 Mar 2020
Edited: Ali Najem on 29 Mar 2020
In fact, there are many functions files but here it is :
dy1dt=[-(-u*A1*y1(1)-u*(A2*y1(2)*v1+ A3*y1(3)*v1+ A4*y1(4)*v2+ A5*y1(5)*v2+ A6*y1(6)+ A7*y1(7)));
-(-u*y1(1)*(yd - 1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1))^2*((2*x1*exp(- w1*x1 - w2*x2)*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w1*x1 - w2*x2) + 1)^3) + (2*v1^2*x1*exp(- w1*x1 - w2*x2)*(x1^2 + x2^2)*(1/(exp(- w1*x1 - w2*x2) + 1) - 1)*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w1*x1 - w2*x2) + 1)^4) + (2*v1*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(1/(exp(- w1*x1 - w2*x2) + 1)^2 + 1/(exp(- w3*x1 - w4*x2) + 1)^2)*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1))/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^4*(exp(- w1*x1 - w2*x2) + 1)^2) + (2*v1^2*x1*exp(- w1*x1 - w2*x2)*(x1^2 + x2^2)*(1/(exp(- w1*x1 - w2*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w1*x1 - w2*x2) + 1)^3) + (2*v1*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(1/(exp(- w1*x1 - w2*x2) + 1)^2 + 1/(exp(- w3*x1 - w4*x2) + 1)^2)*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^3*(exp(- w1*x1 - w2*x2) + 1)^2) + (2*v1^3*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(x1^2 + x2^2)*(1/(exp(- w1*x1 - w2*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1))/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^4*(exp(- w1*x1 - w2*x2) + 1)^4) + (2*v1^3*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(x1^2 + x2^2)*(1/(exp(- w1*x1 - w2*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^3*(exp(- w1*x1 - w2*x2) + 1)^4) + (2*v1*v2^2*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(x1^2 + x2^2)*(1/(exp(- w3*x1 - w4*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^3*(exp(- w1*x1 - w2*x2) + 1)^2*(exp(- w3*x1 - w4*x2) + 1)^2) + (2*v1*v2^2*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(x1^2 + x2^2)*(1/(exp(- w3*x1 - w4*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1))/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^4*(exp(- w1*x1 - w2*x2) + 1)^2*(exp(- w3*x1 - w4*x2) + 1)^2)) - (2*v1*x1*exp(- w1*x1 - w2*x2)*exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1))*(yd - 1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1))*(((1/(exp(- w1*x1 - w2*x2) + 1)^2 + 1/(exp(- w3*x1 - w4*x2) + 1)^2)*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2 + (v1^2*(x1^2 + x2^2)*(1/(exp(- w1*x1 - w2*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w1*x1 - w2*x2) + 1)^2) + (v2^2*(x1^2 + x2^2)*(1/(exp(- w3*x1 - w4*x2) + 1) - 1)^2*(1/(exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1) - 1)^2)/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w3*x1 - w4*x2) + 1)^2)))/((exp(- v1/(exp(- w1*x1 - w2*x2) + 1) - v2/(exp(- w3*x1 - w4*x2) + 1)) + 1)^2*(exp(- w1*x1 - w2*x2) + 1)^2)...
as you can seen this under line equation is aw1 i just copied and pasted then put it in costate fileand all working with no problem with ODE15s but beacuse so long i need way to tranfer as it is
am so grateful to assist me.
This is bit of misleading example. When you copy and paste a symbolic expression in another file, it is not really symbolic. MATLAB just consider them as regular variables. Following code show how to do it properly. In your original code (where you calculate aw1) add the following lines
aw1=diff(aa,w1);
aw1 = matlabFunction(aw1, 'Vars', [w1 w2 w3 w4 v1 v2 x1 x2 yd]);
save('aw1.mat', 'aw1'); % <--- save the function handle.
Now in the function where you need to call it (for example, Costatesimple) load and call it like this
load('aw1.mat');
dy1dt=[-(-u*A1*y1(1)-u*(A2*y1(2)*v1+ A3*y1(3)*v1+ A4*y1(4)*v2+ A5*y1(5)*v2+ A6*y1(6)+ A7*y1(7)));
-(-u*y1(1)*(aw1(w1,w2,w3,w4,v1,v2,x1,x2,yd)))];
%^ aw1 is a function, correct these names according to their names inside Costatesimple
In fact, I would recommend to load this function inside the Costatesimple (since this is called by ode15s several times and loading same file is just waste of computing time). To do it this way, first change the line
function dy1dt = Costatesimple(t,y1,u,x1,x2,yd,x1t,e1,w11,w21,w31,w41,v11,v21,aw1)
% ^ add this input for function handle
and the call ode15s like this
load('aw1.mat');
[T1,cox]=ode15s(@(t,y1) Costatesimple(t,y1,u,x1,x2,yd,x1t,e1,w1,w2,w3,w4,v1,v2,aw1),tspan2,[0 0 0 0 0 0 0],options);
and then you don't need to write the load statement inside Costatesimple. This will be much faster than loading the mat at each call to Costatesimple.
Ali Najem
Ali Najem on 1 Apr 2020
thank you so much bro it worked
i appreciate your time to solve my issue
am so grateful to assisted me.
Glad to be of help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!