区間に応じて2つの関数を使い分けて演算したい

3 views (last 30 days)
kenichiro inagaki
kenichiro inagaki on 18 Feb 2021
Commented: kenichiro inagaki on 19 Feb 2021
例:
x<0ではy=exp(x), x>=0ではy=sin(x) の yを計算で取り出したいです。
plotのしかたまでは見つけられますが、データをワークスペースにどう取り出せばいいのかわかりません。

Accepted Answer

Hernia Baby
Hernia Baby on 18 Feb 2021
y1 = y(x(x<0));
y2 = y(x(x>=0));
のようにすれば取り出せます。
以下は一度正負で異なる関数を返し、まとめてプロットする方法です。
参考にどうぞ。
clear, clc, close all;
x = -3:0.1:3;
x = x';
y1 = exp(x(x<0));
y2 = sin(x(x>=0));
figure(1);
plot(x,[y1; y2],'o')
  3 Comments
Hernia Baby
Hernia Baby on 19 Feb 2021
その認識であってます。
列の統合にはカンマ( , )が必要です。
2列目にyを統合する場合は以下の手順を行ってください。
-----------
y = [y1; y2];
xy = [x, y];
-----------
xy(:,1) に x の列ベクトル、xy(:,2) に y の列ベクトル
がそれぞれ格納されます。
kenichiro inagaki
kenichiro inagaki on 19 Feb 2021
ありがとうございます!できました!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!