sprintf loop for writing equations
Show older comments
i would like to get matlab to write equations automatically which i would like to use in fsolve.
the equations which i would like to print are in this form:
Dtau(i)+2*x(1)*l(i)*(s(i)^2*(cos(x(2))*cos(alpha_1(i))+sin(x(2))*sin(alpha_1(i)))
I've been told to use the command fprintf, and i've done this, which properly prints the equations in a txt file.
FID = fopen('equations list.txt','w');
for i=1:length(p)
% lenghts
l(i)=((q(i,1)-p(i,1))^2+(q(i,2)-p(i,2))^2)^0.5;
% angles
alpha_1(i)=atan((q(i,2)-p(i,2))/((q(i,1)-p(i,1))));;
% times
tau_f(i)=l(i)/(V1+V2);
tau_b(i)=l(i)/(V1-V2);
Dtau(i)=tau_f(i)-tau_b(i);
formatSpec = '%.10f+2*x(1)*%.10f*(%.10f)^2*(cos(x(2))*cos(%.10f)+sin(x(2))*sin(%.10f)) \n';
F(i)=fprintf(formatSpec,Dtau(i),l(i),s,alpha_1(i),alpha_1(i));
end
but then i cannot import with all the commands avaliable from matlab which i looked at for several days (for example sequential use of importdata(file), transpose, and cell2mat insert those brace indexing ' at the end and the beginning which gets the array unusable in fsolve).
so i've been thinking about using sprintf like this
F(i)=sprintf(formatSpec,Dtau(i),l(i),s,alpha_1(i),alpha_1(i));
directly in my code to create the matrix directly without printing in an external file but i get the error "Unable to perform assignment because the left and right sides have a different number of elements" .
So my question is: does anybody know a way to use sprintf in a for loop to write the equations in a matrix with including those parameters?
Any help would be greatly appreciated.
Accepted Answer
More Answers (1)
Jan
on 24 Dec 2022
F{i} = sprintf(formatSpec,Dtau(i),l(i),s,alpha_1(i),alpha_1(i));
%^ ^ Curly braces for a cell string
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!