Evaluating an expression using MATLAB - matlab

I am trying to evaluate a parameter using Matlab , here is the code
miu_not=1.257*1e-6; % permeability of free space
efslon_not=8.854*1e-12;% permittivity of free space
efslon_rg=input('Enter the relative permittivity of ground ')
segma_g=input('Enter the ground conductivity') ;
b=input('enter the conductor radius');
f= input('Enter the frequency');
w=2*pi*f;
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))
It is giving me an error regarding the indices for the last line that it must be positive or logic , while i don't have any array in the equation ???
The exact error message is:
Array indices must be positive integers or logical values.
Error in Untitled (line 8)
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))

In your last statement
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))
is there supposed to be an operator between the miu_not and segma_g?
I think it's evaluating to miu_not(%number%) so Matlab thinks you're trying to index miu_not by number.

Related

Receiving unknown errors when trying to use fsolve to find the solutions of a function

I have to plot the solution for masses between 4 and 8kg, which I wrote using the code below:
weight=linspace(4,8,100)
Then I use a for loop to find the solutions for the weights within this vector, while also declaring the variable a, and a guess of the time required:
for z=1:length(weight)
a=(weight./(1360*pi)).^(1/3)
tguess=14400
timeinseconds=fsolve(#(t)cookingtimes(t,a),tguess)
timeinhours=timeinseconds/(60*60)
end
The function that I mentioned previously is below:
function F=cookingtimes(t,a)
k=1:22;
alpha=0.7*10^(-7);
F(1)=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).*(pi.^2).*((alpha.*t))./(a.^2)))-57/80
end
Where F(1) represents the following equation 1, which will be equal to 57/80. Rather than adding until infinity, I chose the value of 22 as the equation converges to this value. r/a is equal to 0.464, which is why this is present within my function.
When running this code, I am given errors that I cant manage to solve.
Arrays have incompatible sizes for this operation.
Error in Homework1>cookingtimes (line 58)
F(1)=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).*(pi.^2).*.((alpha.*t))./(a.^2)))-57/80
Error in Homework1 (line 28)
timeinseconds=fsolve(#(t)cookingtimes(t,a),tguess)
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
The arrays that I have produced are all of the same length, which is why I am very stuck on this issue.
You are trying to perform an operation on two arrays that have different sizes. In your case, the issue may be caused by the fact that you are defining the variable a as a vector inside the for loop, but using it in the function cookingtimes as a scalar. To fix this, you can modify your function to accept a vector of a values and return a corresponding vector of function values.
I modified the cookingtimes function to accept a vector of a values, and return a corresponding vector of function values.
function F=cookingtimes(t,a)
k=1:22;
alpha=0.7*10^(-7);
F=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).* (pi.^2).*((alpha.*t))./(a.^2)))-57/80;
end
In the for loop, You need to compute a for each weight value, and use a vector of a values in the call to fsolve. Also store the solutions in a vector timeinseconds instead of a scalar, as there are multiple solutions to be found.
for z=1:length(weight)
a=(weight(z)./(1360*pi)).^(1/3); % compute a for each weight value
tguess=14400;
timeinseconds(z)=fsolve(#(t)cookingtimes(t,a),tguess);
timeinhours=timeinseconds/(60*60);
end

Unusual observation during LLyod algorithm implementation in 2D matrix. How can I pinpoint error?

During the implementation of llyod algorithm in 2D matrix, I encountered unusual value assigned by code. The region belonging to same partition is assigned different value, particularly 0 that is causing the problem. How can I pinpoint the error?
The code is written in Matlab.
.
On execution of following custom code in Matlab to implement llyod max quantizer
%This code perform quantization using llyod max quantizer algorithm
%
function [mat2encode,distor,rel_distor,d,r]=llyod_quant(mat2quant_scal_f)
%declaring quantization matrix
quant_mat=zeros(size(mat2quant_scal_f));
bts=input('enter the number of bits to represent decision level for llyod quantizer: ');
lvl=2.^(bts);
[e1,e2]=size(mat2quant_scal_f);
row_vector=reshape(mat2quant_scal_f,[1,e1*e2]);
[M1,N1]=size(row_vector);
tot_num=M1*N1;
[d, r, distor,rel_distor] = lloyds(row_vector,lvl);
for k=1:lvl-1
if k==size(d,2)
[v1,v2]=find(mat2quant_scal_f>=d(k-1) & mat2quant_scal_f<d(k));
for j=1:length(v1)
quant_mat(v1(j),v2(j))=round(r(k));
end
else
[v1,v2]=find(mat2quant_scal_f>=d(k) & mat2quant_scal_f<d(k+1));
for j=1:length(v1)
quant_mat(v1(j),v2(j))=round(r(k));
end
end
end
mat2encode=quant_mat;
end
The output for element (1,33) and (9,33) element of matrix is (89) and (0) as shown below:
The code should have assigned the output 89 to both elements but that's not the case. I am perplexed.
Edit
Input matrix is uploaded here.

Array indices must be positive integers or logical value when read images in matlab

trying to read multi images in matlab in loop so error Array indices must be positive integers or logical values is appear
for i=1:12 % we have 16 images we have in or folder
clc;clear;
images ='/home/mohamed/Downloads/Lab6-20200415/Lab6-20200421/Lab6/Lab6/';
jpgfiles=dir(fullfile(images,'\*.jpg*'));
n=numel(jpgfiles(i));
im=jpgfiles(i).name;
im1=imread(fullfile(images,im));
d1 = rg2gray(iml);
imshow(im1);
end
Here is a minimal example demonstrating your problem:
for i=1:10
clear
jpgfiles=rand(10,1); %some dummy data to replace your actual code
jpgfiles(i);
end
the clear deletes the variable i which means i is the imaginary unit, not a valid index. Avoid the variable name i, it leads to difficult to debug problems. With other variable names, the much clearer error message would have been:
Unrecognized function or variable 'ix'.
Error in foo (line 4)
jpgfiles(ix);

Invalid setting for parameter 'Gain'

I am trying to create a simple controller, but keep receiving an error for the gain block stating that there is an undefined variable 'u'. I do not understand Simulink well and I am just trying to replicate an old homework problem right now. The code I have here was provided as a solution, but I still receive the error when I try to run it. Any insights as to what might be going on?
I= 10; Wl= 5; k= 2; J= 1;
%set initial conditions
thetaIC= 0; phiIC= 0; x0= zeros(4,1);
%fix theta= 0, check output
[xe, ue]= trim('Ex3_System',x0,0,x0,1)
[A,B,~,~]= linmod('Ex3_System', xe, ue)
%choose your desired poles
p= linspace(-1.2,-1.5,4)
%recall the minus sign
K= -acker(A,B,p)
%perturb initial condition
thetaIC= deg2rad(5);
sim('Ex3_Controller');
Invalid setting in 'Ex3_Controller/Gain' for parameter 'Gain'. Caused
by:
Error using hw12 (line 57)
Error evaluating parameter 'Gain' in 'Ex3_Controller/Gain'
Error using hw12 (line 57)
Undefined function or variable 'u'.
Error using hw12 (line 57)
Variable 'u' does not exist.
Suggested Actions:
• Load a file into 'Base Workspace'. - Fix
• Create a new variable. - Fix
Update: After removing the u term from the gain block, I received a different error:
Error using hw12 (line 57)
Error in port widths or dimensions. Output port 1 of 'Ex3_Controller/Gain' is a one dimensional vector with 4 elements.
Error using hw12 (line 57)
Error in port widths or dimensions. Input port 1 of 'Ex3_Controller/Model1' is a one dimensional vector with 1 elements.
The Gain block takes the value of the input signal and multiplies it by the value of the gain. In your case the gain is K and that is all you need to put into the gain block (i.e. remove the *u, Simulink handles that for you.)
Once that is done, the dimension error you are getting is because your controller requires u to be a scalar, but you are feeding a 4 element vector into it. You need to change the appropriate parameter of the Gain block so that it does a matrix multiplication, taking the 4-by-1 matrix K and (matrix) multiplying it with the 4 element "out" signal to produce a scalar.

MATLAB: Using FZERO on a function which has a vector output

I am working on my thesis and running in some programming problems in Matlab. I am trying to implement the ''golden Bisection Method'' to speed up my code. To this end, I've consulted the build in function FZERO.
So I am determining the difference between two vectors which are both (1x20).
Difference = Clmax_dist-cl_vec;
Clmax_dist comes from a semi-empirical method and cl_vec comes from the excecution of an external AVL.exe file.
Essentially, this difference depends only on one single variable AOA because the Clmax_dist vector is a constant. Hence, I am constantly feeding a new AOA value to the AVL.exe to obtain a new cl_vec and compare this again to the constant Clmax_dist.
I am iterating this until one of the element in the vector becomes either zero or negative. My loop stops and reveals the final AOA. This is a time consuming method and I wanted to use FZERO to speed this up.
However, the FZERO documentation reveals that it only works on function which has a scalar as input. Hence, my question is: How can I use FZERO with a function which has a vector as an output. Or do i need to do something totally different?
I've tried the following:
[Difference] = obj.DATCOMSPANLOADING(AOA);
fun=#obj.DATCOMSPANLOADING;
AOA_init = [1 20];
AOA_root = fzero(fun,AOA_init,'iter');
this gave me the following error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 423)
while fb ~= 0 && a ~= b
Error in CleanCLmax/run (line 11)
AOA_root = fzero(fun,AOA_init,'iter');
Error in InitiatorController/moduleRunner (line 11)
ModuleHandle.run;
Error in InitiatorController/runModule (line 95)
obj.moduleRunner(ModuleHandle);
Error in RunSteps (line 7)
C.runModule('CleanCLmax');
The DATCOMSPANDLOADING function contains the following:
function [Difference] = DATCOMSPANLOADING(obj,AOA)
[Input]= obj.CLmaxInput; % Creates Input structure and airfoil list
obj.writeAirfoils(Input); % Creates airfoil coordinate files in AVL directory
[Clmax_dist,YClmax,Cla_mainsections] = obj.Clmax_spanwise(Input); % Creates spanwise section CLmax with ESDU method
[CLa] = obj.WingLiftCurveSlope(Input,Cla_mainsections); % Wing lift curve slope
[Yle_wing,cl_vec] = obj.AVLspanloading(Input,CLa,AOA); % Creates spanloading with AVL
Difference = Clmax_dist-cl_vec;
end
If I need to elaborate further, feel free to ask. And of course, Thank you very much.
fzero indeed only works on scalars. However, you can turn your criterion into a scalar: You are interested in AOA where any of the elements in the vector becomes zero, in which case you rewrite your objective function to return two output arguments: minDifference, which is min(Difference), and Difference. The first output, minDifference is the minimum of the difference, i.e. what fzero should try to optimize (from your question, I'm assuming all values start positive). The second output you'd use to inspect your difference vector in the end.