How can I plot graph using external function?
6 views (last 30 days)
Show older comments
I am trying to plot this equation.
I have the values of B, omega_d, zeta, N_k and t_j is the multiples of 1.89.
First, I tried make an external function to represent the equation including sigma and deduce a function about t. Then tried to draw a gragh for t in conclution.
But I am struggling with where to define t_j, call the external function, and making a function about t and plotting it. It's the code that I wrote below.(there will be lots of errors but don't know how to fix it..)
Any comment will be helpfull. Please help, thanks!
<plotting>
clc; clear;
t=0:10;
sum=s(t)
figure(1)
fplot(s, [1,10])
<s.m>
function [sum]= s(t)
B_n=-5.36 ;
omega_d= 400;
zeta=29 ;
N_k=10;
% sum=zeros(1,N_k);
for i=1:N_k
t_j= 1.89.*i;
sum= sum+ exp(-(zeta/2).*(t-t_j)).*(-zeta*(sin(omega_d/2.*(t-t_j)))+ omega_d.*cos(omega_d/2*(t-t_j)));
end
sum=B_n/omega_d.*sum;
end
1 Comment
Rik
on 13 Mar 2023
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable. I also swapped the order of the two code sections so it can be run directly in this forum.
Accepted Answer
Rik
on 13 Mar 2023
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
In this case the first thing to fix is your usage of sum as a variable name. You really should avoid function names when picking a variable name. If you use sum later on, will you remember that you made it a variable instead of the function?
The next thing is to add an initial value to your output, which we will make 0.
Lastly, when using fplot you need to provide a function handle. You called the function directly instead. You also didn't use any of the previously calculated values. Below I show two ways to plot this function, along with the other corrections to your code.
t=0:10;
sum_value=s(t);
figure(1)
subplot(1,2,1)
fplot(@s,[0 10])
subplot(1,2,2)
plot(t,sum_value) % you can increase level of detail with t=linspace(0,10,50);
function sum_value= s(t)
% Write an explanation of this function here, along with example usage.
B_n=-5.36 ;
omega_d= 400;
zeta=29 ;
N_k=10;
sum_value=0;
for i=1:N_k
t_j= 1.89.*i;
sum_value= sum_value+ exp(-(zeta/2).*(t-t_j)).*(-zeta*(sin(omega_d/2.*(t-t_j)))+ omega_d.*cos(omega_d/2*(t-t_j)));
end
sum_value=B_n/omega_d.*sum_value;
end
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!