Info

この質問は閉じられています。 編集または回答するには再度開いてください。

help for fprintf my code

1 回表示 (過去 30 日間)
Murad Alzahrani
Murad Alzahrani 2019 年 7 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
function [out] = slope(t,G)
k1=10;k2=20;m1=2;m2=4;
v1=G(1); x1=G(2);v2=G(3);x2=G(4);
out(1)=(-k1*x1+k2*(x2-x1))/m1;
out(2)=v1;
out(3)=-k2*(x2-x1)/m2;
out(4)=v2;
end
and
clc;clear;
tf=10;
n=1;
dt=0.1;
t(n)=0.1;
m(n,:)=[1 0 -1 0];
while t(n)<tf
t(n+1)=t(n)+dt;
m(n+1,:)=m(n,:)+slope(t(n),m(n,:))*dt;
n=n+1;
end
t
m
I would like make fprintf to show me t and m as colums. how?
note : m has 4 colums.
Thank you

回答 (1 件)

Adam Danz
Adam Danz 2019 年 7 月 17 日
編集済み: Adam Danz 2019 年 7 月 19 日
"I would like make fprintf to show me t and m as colums"
...where t is an [1 x n] vector and m is a [n x 4] matrix.
fprintf('%.2f %.2f %.2f %.2f %.2f\n', [t.',m].') %note the two transposes
First 5 rows:
0.10 1.00 0.00 -1.00 0.00
0.20 1.00 0.10 -1.00 -0.10
0.30 0.75 0.20 -0.90 -0.20
0.40 0.25 0.28 -0.70 -0.29
0.50 -0.45 0.30 -0.42 -0.36
Depending on what you're doing, a table may be a better approach.

Community Treasure Hunt

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

Start Hunting!

Translated by