# I need to send variables to solve(). How do I do it?

1 view (last 30 days)
Kushagra Vidyarthi on 27 Feb 2015
Edited: Star Strider on 27 Feb 2015
Hello,
I have to send 3 variables to solve(). I have defined two of these variables such that their values are precomputed in a loop. The value changes every loop. So I need to define the as variables. I need to use the solve command to get the value of the third variable. But I cannot do that and the output is a symbol. I wrote the following loop to do this.
for i = 1:1:60
syms lambda;
a = (V(i)*cos(D(i)/W))/(omega*R);
b = (V(i)*sin(D(i)/W))/(omega*R);
temp = solve(Ct-(2*lambda*sqrt(a^2 + (b+lambda)^2))==0,lambda);
end
V,D are variables with a value changing every iteration of the loop (predefined). W, omega,Ct and R have constant values.
Help appreciated. Thanks
Kushagra

Star Strider on 27 Feb 2015
Edited: Star Strider on 27 Feb 2015
I would use fzero:
Ct = randi(100); % Pick A Number
R = randi(50); % Pick A Number
W = randi(10); % Pick A Number
omega = 2*pi*rand; % Pick A Number
V = randi(10, 1, 60); % Pick A Randonm Vector
D = 2*pi*rand(1,60); % Pick A Randonm Vector
for k1 = 1:length(V)
a = (V(k1)*cos(D(k1)/W))/(omega*R);
b = (V(k1)*sin(D(k1)/W))/(omega*R);
fcn = @(lambda) Ct-(2*lambda*sqrt(a^2 + (b+lambda)^2));
est_lambda(k1) = fzero(fcn, randi(10));
end
It’s best to not use ‘i’ and ‘j’ as variables, including as loop counters. MATLAB uses them for its imaginary operators, and using them otherwise can cause confusion.