How to call a variable matrix from one script to another script
18 views (last 30 days)
Show older comments
Bathala Teja
on 7 Sep 2021
Commented: Bathala Teja
on 8 Sep 2021
I have two scripts. In first one i will get A and B matrices which are interms of theta as shown below.
sym theta
A = cos(theta).*[1 5 6;
2 9 3;
5 1 0]
B = tan(2*theta).*[8 5 1;
0 1 6;
2 4 7]
I want to use these A and B matrices in another script, which is given below
function dydt = scriptname(t, y)
theta = y(3); % Substitute y(3) in place of theta in A and B matrices
A % Extract A matrix
B % Extract B matrix
V = [1;
5;
3]
dydt = V-((A+B)*Y);
end
Finally i will plot these ODE's in another script that i didnt gave here.
My main doubt is how to extract A and B matrices(especially those are variable matrices) into another script.
After extracting these A and B matrices i will substitute y(3) so that i can get rid of theta.
I havent shown my exact problem because its too complicated. Focus on my main doubt dont care about values.
Thanks in advance.
0 Comments
Accepted Answer
Fabio Freschi
on 7 Sep 2021
I am not sure I have understtod correctly your problem. I give it a try.
I would add an extra layer using an anonymous function to solve the porblem of passing A and B to your funciton
clear variables, close all
% anonymous functions to calculate A and B
A = @(theta)cos(theta).*[1 5 6; 2 9 3; 5 1 0];
B = @(theta)tan(2*theta).*[8 5 1; 0 1 6; 2 4 7];
% extra layer to pass also A and B to your dydt function
myfun = @(t,y)scriptname(t,y,A,B);
% dummy values for tspan and y0
tspan = [0 1];
y0 = [0; 0; 0];
% ode solver
sol = ode45(myfun,tspan,y0);
function dydt = scriptname(t,y,A,B)
theta = y(3); % Substitute y(3) in place of theta in A and B matrices
V = [1; 5; 3];
% actual calculation of A and B with the current value for theta
dydt = V-((A(theta)+B(theta))*y);
end
18 Comments
Fabio Freschi
on 8 Sep 2021
You should ask another question, because this is beyond the scope of the original post. If the answer solves your problem, please accept it.
It is pretty simple to test yourself your code, creating a random y matrix. You can also run your code in this window using the run comamnd.
More Answers (1)
Jan
on 7 Sep 2021
Edited: Jan
on 7 Sep 2021
The first code shows a "script", the second one a "function". While scripts share their variables with the callers automatically, functions don't. As far as I understand all you havt to do is to call the script:
% Script file: "yourScript.m"
sym theta
A = cos(theta).*[1 5 6;
2 9 3;
5 1 0]
B = tan(2*theta).*[8 5 1;
0 1 6;
2 4 7]
% Function file: "scriptname.m" ( missleading name! This is not a script.)
function dydt = scriptname(t, y)
theta = y(3); % Substitute y(3) in place of theta in A and B matrices
yourScript; % <-- A and B are defined internally
V = [1;
5;
3]
dydt = V-((A+B)*Y);
end
For productive code, scripts are a shot in your knee, because you cannot see directly, where the variables are defined. Use functions instead, to forward variables explicitly:
% Script file: "yourFunction.m"
function [A, B] = yourFunction()
sym theta
A = cos(theta).*[1 5 6;
2 9 3;
5 1 0]
B = tan(2*theta).*[8 5 1;
0 1 6;
2 4 7]
end
% Function file: "theOtherFunction.m"
function dydt = theOtherFunction(t, y)
[A, B] = yourFunction();
V = [1;
5;
3]
dydt = V-((A+B)*Y);
end
7 Comments
Fabio Freschi
on 7 Sep 2021
when you create A you create B s well, so you must check only one of them
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!