- Create symbolic variables using syms.
- Build the expression need to be evaluated.
- Through an iteration substitute all the variables using subs function. It will output an array.
- Finally, Evaluate the summation of the expression using sum function.
Info
此问题已关闭。 请重新打开它进行编辑或回答。
How to generate a loop for the problem mentioned in the context
1 次查看(过去 30 天)
显示 更早的评论
Dear, scholars,
Anybody can help me with writing a decent code for finding the below equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/283485/image.png)
I can write the code for the second coefficient of numerator (I mean the following part):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/283486/image.png)
But I can not extend it to the whole equation,
Any ideas?
I attach my code for the second part:
clc
clear all
close all
we = linspace(0,100,101);
gh = [1 3 5];
w = [21 39 88];
for i = 1:3
for j = 1:length(we)
a(i,j) = gh(i) * we(j);
C(i,j) = (1/12)+1i*we(j);
x(i,j) = 12*gh(i);
s3(i,j) = 1i*gh(i)*we(j);
s2(i,j) = w(i)^2 - we(j)^2+1i*w(i);
s1(i,j) = 1i*gh(i)*we(j);
v2(i,j) = s3(i,j)/s2(i,j)+C(i,j);
v1(i,j) = s1(i,j)/s2(i,j);
v(i,j) = abs(v1(i,j)/v2(i,j))
d = log10(sum(v))
d1 = sum(v);
end
end
figure
plot(we,d,'LineWidth',2)
grid on
0 个评论
回答(1 个)
Guru Mohanty
2020-4-13
Hi, I understand you are getting issues in calculating sum of the given expression. You can do this using MATLAB Symbolic Toolbox-
Here is a sample code using random values as input.
n=3;
syms s 3
syms C x a
% Create Random Data
av=rand(1,n);
s1_1v=rand(1,n);
s2_1v=rand(1,n);
s3_1v=rand(1,n);
Cv=rand(1,1);
xv=rand(1,n);
% Set Up The Expression
Var=sum(s(1)/s(2))/(C +sum(s(3)/s(2)));
toSum=(Var*x + a)/s(2);
disp(toSum);
for i=1:n
% Substitute Values through Iteration
subSum(i)=subs(toSum,{a , s1_1 , s2_1 , s3_1 , C , x}, ...
{av(i),s1_1v(i),s2_1v(i),s3_1v(i),Cv,xv(i)});
end
OutSum=double(sum(subSum));
disp(OutSum);
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!