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

41 ビュー (過去 30 日間)
Bathala Teja
Bathala Teja 2021 年 9 月 7 日
コメント済み: Bathala Teja 2021 年 9 月 8 日
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.
Bathala Teja
Bathala Teja 2021 年 9 月 8 日
Ok thank you

サインインしてコメントする。

その他の回答 (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 件のコメント
Fabio Freschi
Fabio Freschi 2021 年 9 月 7 日
when you create A you create B s well, so you must check only one of them
Bathala Teja
Bathala Teja 2021 年 9 月 7 日
ok thank you for your explanation

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver 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