Replacing sym with function handle

I gave my script below. In order to get my result i used 'sym' function.
But i want to implement without symbolic math toolbox, how to implement the same script with function handles?
syms phi
w = 2;
Nr = 20;
nr = sym(zeros(1, Nr));
wr = sym(zeros(1, Nr));
for n = 1:Nr
for i = 1:w
Awr = 50*(cos(i*2)-cos(i*9));
nr(1, n) = nr(1, n)+Awr*cos(i*(phi+2*n));
end
wr(1, n) = nr(1, n);
end
NR = vpa(nr+(56*3/2), 4)
NR = 
WR = vpa(wr, 4)
WR = 

답변 (1개)

Alan Stevens
Alan Stevens 2021년 10월 5일

0 개 추천

Do you mean something like this?
phi = pi/3;
[nr, wr] = fn(phi);
disp(phi)
1.0472
disp(nr(4)+56*3/2)
13.1554
disp(wr(4))
-70.8446
function [nr, wr] = fn(phi)
w = 2;
Nr = 20;
nr = zeros(1, Nr);
wr = zeros(1, Nr);
for n = 1:Nr
for i = 1:w
Awr = 50*(cos(i*2)-cos(i*9));
nr(1, n) = nr(1, n)+Awr*cos(i*(phi+2*n));
end
wr(1, n) = nr(1, n);
end
end

댓글 수: 4

Bathala Teja
Bathala Teja 2021년 10월 5일
I want nr and wr are interms of phi as shown in my script.
But here you are assigning value to the phi.
Alan Stevens
Alan Stevens 2021년 10월 5일
Your question specified not using the symbolic toolbox!
I chose a single value of phi as an example. You can pass as many values of phi as you like to the function, by using a loop.
Bathala Teja
Bathala Teja 2021년 10월 5일
After forming these set functions in nr, i want to integrate all the functions w.r.t phi individually.
If i dont get in function handles then how to integrate?
You can use trapz. For example:
philo = 0;
phihi = pi;
phi = linspace(philo, phihi, 100);
dphi = (phihi-philo)/99;
nr = zeros(numel(phi),20);
wr = zeros(numel(phi),20);
Integral_nr = zeros(1,20);
Integral_wr = zeros(1,20);
for m = 1:numel(phi)
[nr(m,:), wr(m,:)] = fn(phi(m));
end
for k = 1:20
Integral_nr(k) = trapz(nr(:,k))*dphi;
end
plot(1:20,Integral_nr,'--o'),grid
xlabel('1:Nr'), ylabel('Integrals of nr')
function [nr, wr] = fn(phi)
w = 2;
Nr = 20;
nr = zeros(1, Nr);
wr = zeros(1, Nr);
for n = 1:Nr
for i = 1:w
Awr = 50*(cos(i*2)-cos(i*9));
nr(1, n) = nr(1, n)+Awr*cos(i*(phi+2*n));
end
wr(1, n) = nr(1, n);
end
end

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2021b

태그

질문:

2021년 10월 5일

댓글:

2021년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by