MATLAB rookie help, How do i create a code to return these outputs?

2 ビュー (過去 30 日間)
Tessa
Tessa 2021 年 4 月 26 日
編集済み: Tessa 2021 年 4 月 26 日
My^''+By^'+Ky=x(t)
K = spring constant
B = dampening constant
M = mass
T = time
X = forcing function
Y = position of the mass
If you are given K, B, and M along with a table that gives values of x for a given t;
t (sec) [ 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2]
x ( kg m/s^2) [0 0 0.1 0.2 0.3 0.18 0.1 0 0 0 0]
Write a program that K, B and M are constant and supplied by the user input. Use T as the ending time value that solutions are sought ( also entered by the user when calling the function.
Initial values are t = 0 , y(0) = 0 , y’(0) = 0
The program outputs a flag ( runs without issue flag = 0) , t (vector of time values), y ( vector of position values), and yp ( vector of derivative values).

回答 (1 件)

Steven Lord
Steven Lord 2021 年 4 月 26 日
You've omitted a function declaration line at the top of the file. That will turn the file from a script file (which cannot return outputs) into a function file (which can.) Use the declaration of the myfun function as a model.
To return multiple outputs, use square brackets like the myfun function below.
[a, b] = myfun(5)
a = 25
b = 4
function [out1, out2] = myfun(in1)
out1 = in1.^2;
out2 = in1-1;
end
See this documentation page for more information.
  1 件のコメント
Tessa
Tessa 2021 年 4 月 26 日
That is just a sample program. Not what I’m trying to run. It’s just showing how to pass parameters to a sub function. I need a code to implement the equation based on values from the table

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

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by