How can I solve a equation when the constant numbers change when the variable changes?
4 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1325060/image.png)
How can I change the code below if there is a relation between N_k, t, t_j. The relation is that N_k is the integer of t/tt.(when I define t_j=tt*N_k)
I'm confused how I can get the each value when t is changing within the range and apply the effects on the other components.(I tried 'while' and 'if' but I got a warning messege 'Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.')
syms t
B_n = -5.36;
omega_d = 400;
zeta = 29;
N_k =10;
tt=0.001;
points = zeros(1, N_k, 'sym');
for i=1:N_k
t_j= tt.*i;
points(i) = exp(-(zeta/2).*(t-t_j)).*(-zeta*(sin(omega_d/2.*(t-t_j)))+ omega_d.*cos(omega_d/2*(t-t_j)));
end
total = B_n/omega_d.*sum(points);
fplot(total, [0 2])
0 个评论
回答(1 个)
Yash
2023-3-28
Hi Sojung,
If N_k is related to t such that N_k is the integer part of t/tt, then you can compute N_k inside the loop for each value of t and use it to compute t_j. Here's how you can modify your code to incorporate this relation:
syms t
B_n = -5.36;
omega_d = 400;
zeta = 29;
N_k =10;
tt=0.001;
points = zeros(1, N_k, 'sym');
for t_val=0:0.001:2
N_k = floor(t_val/tt);
t_j= tt.*N_k;
point = exp(-(zeta/2).*(t_val-t_j)).*(-zeta*(sin(omega_d/2.*(t_val-t_j)))+ omega_d.*cos(omega_d/2*(t_val-t_j)));
points(N_k+1) = points(N_k+1) + point;
end
total = B_n/omega_d.*sum(points);
fplot(total, [0 2])
In this modified code, we loop over t_val values from 0 to 2 with a step of 0.001. For each t_val value, we compute the corresponding N_k value using floor(t_val/tt) and compute t_j using tt*N_k. We then compute the point value for the current t_val using the t_j value and add it to the points array at the index N_k+1 (since the array indices start from 1 in MATLAB). Finally, we compute the total value as before and plot it over the range [0 2].
Note that in this modified code, we use t_val instead of t as the loop variable to avoid confusion with the t symbol that is defined using syms t. Also, we use the + operator to accumulate the point values at the appropriate index in the points array instead of assigning them directly to the array, since multiple points may correspond to the same index.
I hope this helps :)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!