How to call a variable matrix from one script to another script

41 次查看(过去 30 天)
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.

采纳的回答

Fabio Freschi
Fabio Freschi 2021-9-7
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 个评论
Fabio Freschi
Fabio Freschi 2021-9-8
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.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2021-9-7
编辑:Jan 2021-9-7
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by