Syntax problem in creating a function which takes a vector as an argument

3 vues (au cours des 30 derniers jours)
Jenni
Jenni le 1 Fév 2024
Commenté : Jenni le 1 Fév 2024
r_1 = 0.84 - 0.29i;
r_2 = 0.84 + 0.29i;
c = 4.55;
t = [2:1:50];
y(t) = k_1*r_1^t + k_2*r_2^t + c;
function out = t
out = [y(t)];
end
Hey! I have a syntax problem with my function:
I am trying to write a code which takes a vector [t_1,..,t_n] as an argument and returns a vector [y(t_1),...,y(t_n)]. Here you can see my function for now and the parameters which the function should have. How I should write the working code?

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 1 Fév 2024
Modifié(e) : Dyuman Joshi le 1 Fév 2024
Use element-wise power - power, .^ and define the expression as an anonymous function -
r_1 = 0.84 - 0.29i;
r_2 = 0.84 + 0.29i;
c = 4.55;
t = 2:1:50;
%Sample values for example
k_1 = 1;
k_2 = 2;
y = @(t) k_1*r_1.^t + k_2*r_2.^t + c;
out = y(t)
out =
Columns 1 through 10 6.4145 + 0.4872i 5.6923 + 0.5895i 4.9967 + 0.6056i 4.3984 + 0.5519i 3.9425 + 0.4489i 3.6491 + 0.3184i 3.5163 + 0.1803i 3.5248 + 0.0516i 3.6440 - 0.0558i 3.8375 - 0.1344i Columns 11 through 20 4.0684 - 0.1818i 4.3037 - 0.1993i 4.5165 - 0.1912i 4.6882 - 0.1639i 4.8086 - 0.1243i 4.8754 - 0.0794i 4.8924 - 0.0352i 4.8683 + 0.0035i 4.8143 + 0.0337i 4.7427 + 0.0539i Columns 21 through 30 4.6650 + 0.0639i 4.5910 + 0.0648i 4.5281 + 0.0584i 4.4808 + 0.0469i 4.4511 + 0.0327i 4.4384 + 0.0179i 4.4407 + 0.0043i 4.4545 - 0.0070i 4.4758 - 0.0151i 4.5008 - 0.0198i Columns 31 through 40 4.5260 - 0.0214i 4.5484 - 0.0203i 4.5664 - 0.0172i 4.5787 - 0.0129i 4.5853 - 0.0080i 4.5867 - 0.0033i 4.5837 + 0.0007i 4.5777 + 0.0039i 4.5699 + 0.0059i 4.5615 + 0.0069i Columns 41 through 49 4.5537 + 0.0069i 4.5471 + 0.0062i 4.5422 + 0.0049i 4.5392 + 0.0034i 4.5380 + 0.0018i 4.5384 + 0.0003i 4.5399 - 0.0009i 4.5423 - 0.0017i 4.5450 - 0.0022i
  1 commentaire
Jenni
Jenni le 1 Fév 2024
Excellent! Using the - power, .^ and defining the expression as an anonymous function was very helpful idea! Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Power and Energy Systems dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by