repeating user created functions

1 回表示 (過去 30 日間)
Matthew
Matthew 2023 年 9 月 24 日
移動済み: Dyuman Joshi 2023 年 9 月 25 日
I have a nested function d_1 that i'm trying to run for the values 1-10. I need to store the data so i can plot it.
I tried d_1(1:10) but this is just displaying a single value (1)
would a for loop work for this?
something like:
N = 10
x = zeros(length(N))
for d_1 =
I'm lost from here, any pointers or help would be appreciated.
  2 件のコメント
Hiro Yoshino
Hiro Yoshino 2023 年 9 月 25 日
Could you elaborate on this?
Matthew
Matthew 2023 年 9 月 25 日
I will try my best, I'm slowely getting it to work.
right now i have
N = 10;
for k = 1:N
d_1(k)
end
this displays the answers i need in the command window, but i need each one of these answers stored in a row vector so i can plot them.

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

回答 (2 件)

Matthew
Matthew 2023 年 9 月 25 日
移動済み: Dyuman Joshi 2023 年 9 月 25 日
i believe i figured it out
N = 1:10;
s = zeros(1,length(N));
for k = 1:length(N)
s(k) = d_1(k)
end
plot(s)
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2023 年 9 月 25 日
移動済み: Dyuman Joshi 2023 年 9 月 25 日
Yes

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


Pratyush
Pratyush 2023 年 9 月 25 日
I understand that you want to run the nested function 'd_1' for the values 1-10 and store the data for plotting.
You can use a loop to iterate over the values and store the results in an array. Here is an example script:
% Initialize an empty array to store the data
data = [];
% Iterate over the values 1-10
for i = 1:10
% Call the nested function and store the result in the array
data(i) = d_1(i);
end
% Now you can plot the data
plot(1:10, data)
xlabel('x')
ylabel('d_1(x)')
% Define the nested function d_1
function result = d_1(x)
% Your nested function logic here
result = x^2; % Just an example, replace with your actual logic
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by