How to create a block diagram for a script in matlab - matlab

I was wondering if I have any chance to create a logical schema(state diagram) in matlab for a script. Or if I could find any programs that would do that for me.
for something like this:
A=[];
n=input('Numarul de linii & coloane al matricei A: ');
for i = 1:n
for j = 1:n
str = ['Introduceti elementul de pe randul ' num2str(i) ', coloana ' num2str(j) ': '];
A(i,j) = input(str);
end
end
A
suma=0;
for i = 1:n
for j = 1:n
if (i+j)<=(n) && A(i,j)<0;
suma = suma + A(i,j);
end
end
end
suma

Related

Matlab Picard Method - Assigning existing symfun to a Vector

I am working on a program for the picard method in matlab. This requires multiple iterations over a function being substituted in a to be integrated polynomial.
Now I have an existing polynomial with syms x, which is defined by some vector a:
for i = 1:degree+1
polynomial = symfun(polynomial + a(i) *x^(i-1), x);
end
syms t;
y = symfun( zeros(1,maxIterations+1),x);
y(1) = polynomial;
for i = 2: maxIterations
substitute = subs(polynomial, x, y(i-1));
y(i) = symfun(x0 + int( substitute, t), x); %for some predefined x0
end
But it fails when I try to use y(1) = symfun(polynomial,x);, giving me an error:
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
But as polynomial is just a symfunction, why can't it be assigned to a slot in the vector y? I already tried using feval() and not using symfun() whilst defining, but nothing worked so far.
Complete code:
%%Input
prompt = 'What is the degree of the polynomial? \n ';
degree = input(prompt);
a = zeros(degree+1,1);
for i = 1:degree+1
a(i) = input(['Enter value of coefficient a_' num2str(i-1) ': ']);
end
prompt = 'What is the number of iterations? \n ';
maxIterations = input(prompt);
prompt = 'What is the value of x0? \n ';
x0 = input(prompt);
%%Computing Integrated Vector
aInt = [0];
for i = 1:degree+1
aInt(i+1) = a(i)/(i);
end
fprintf('x0');
for i = 1: degree+1
fprintf( [' + ' num2str(aInt(i+1)) ' x^' num2str(i)] );
end
fprintf('\n');
syms x;
polynomial = 0;
for i = 1: degree+1
polynomial = symfun(polynomial + a(i) * x ^(i-1), x);
end
%Picard Method
syms t;
syms y;
y = symfun( zeros(maxIterations+1,1), x );
y(1) = polynomial;
for i = 2: maxIterations
substitute = subs(polynomial, x, y(i-1));
y(i) = symfun(x0 + int( substitute,t ), t);
end
for i = 1:maxIterations
fprintf(['x_' num2str(i-1) ' = ' y(i), ' \n']);
end
Thanks in advance!
Fixed it!
This is the working code. (might still have slights errors in it). The mistake was not properly defining polynomial, as I first had syms(polynomial) which caused the sum of the polynomial to include `polynomial' as a variable. Also fixed some slight other errors considering the picard method such as the definition of y(1).
%%Input
prompt = 'What is the degree of the polynomial? \n ';
degree = input(prompt);
a = zeros(degree+1,1);
for i = 1:degree+1
a(i) = input(['Enter value of coefficient a_' num2str(i-1) ': ']);
end
prompt = 'What is the number of iterations? \n ';
maxIterations = input(prompt);
prompt = 'What is the value of x0? \n ';
x0 = input(prompt);
%%Computing Integrated Vector
aInt = [0];
for i = 1:degree+1
aInt(i+1) = a(i)/(i);
end
%%vector print
fprintf('x0');
for i = 1: degree+1
fprintf( [' + ' num2str(aInt(i+1)) ' x^' num2str(i)] );
end
fprintf('\n');
syms x;
polynomial = 0;
for i = 1: degree+1
polynomial = polynomial + symfun(a(i) * x ^(i-1), x);
end
%Picard Method
syms t;
syms y;
y = sym( 'y', [1 maxIterations] );
y(1) = x0;
for i = 2: maxIterations
substitute = subs(polynomial, x, y(i-1));
y(i) = symfun(x0 + int( substitute, t ), t);
end
for i = 1:maxIterations
fprintf('The iteration y_%d = %s \n',(i-1), y(i))
end
%plot graphs

Simple MATLAB lexer program

I created a simple lexer program from MATLAB code where, when the user types a string, the lexemes in that string are categorized. However, when I enter a string in the command window the identifiers are not getting displayed.
The code is as follows :
function determineLexemes()
j = 0;
prompt = 'Enter string : ';
str = input(prompt);
arr = char(str);
strTwo = '';
display('Symbol Table');
fprintf('Lexeme \t\t Token \n');
k = length(arr);
for i = 1: k
if(arr(i) == '+')
fprintf('+ \t\t ADD_OP \n');
end
if(arr(i) == '-')
fprintf('- \t\t SUB_OP \n');
end
if(arr(i) == '*')
fprintf('* \t\t MULT_OP \n');
end
if(arr(i) == '/')
fprintf('/ \t\t DIV_OP \n');
end
if(arr(i) == '(')
fprintf('( \t\t LEFT_PAREN \n');
end
if(arr(i) == ')')
fprintf(') \t\t RIGHT_PAREN \n');
end
if(arr(i) == '=')
fprintf('= \t\t EQUAL_OP \n');
end
x = str2num(arr(i));
y = isletter(arr(i));
if(y || (isempty(x) ==0))
strTwo = strcat(strTwo,arr(i));
end
if(~ischar(arr(i)) && ~isnumeric(arr(i)))
if(~isspace(arr(i)) && ~isempty(strTwo))
m(j) = strTwo;
if(isNumeric(strTwo(1)) && regexp('.*[a-zA-]+.*'))
disp(strcat('Error. Potential variable (', strTwo, ') whose name starts with digit found'));
strTwo = '';
j = j + 1;
end
if(~(isNumeric(strTwo(1) && regexp('.*[a-zA-]+.*'))))
disp(strcat(m(j), ('\t\t IDENTIFIER')));
strTwo = '';
j = j + 1;
end
end
end
end
end
And the intended output, when '(2a + b)' is entered to the user prompt,is as follows:
However, the output currently does not identify identifiers (i.e. 2a and b in this example).
Any help on this problem is appreciated.
I tried to keep the changes needed by your code to a minimum, but there were quite a number of mistakes (even things like isNumeric instead of isnumeric or a missing argument for the regex function).
Hope you'll be satisfied with this.
function determineLexemes()
j = 1;
prompt = 'Enter string : ';
str = input(prompt);
arr = char(str);
strTwo = '';
display('Symbol Table');
fprintf('Lexeme \t\t Token \n');
k = length(arr);
for i = 1: k
if(arr(i) == '+')
fprintf('+ \t\t ADD_OP \n');
end
if(arr(i) == '-')
fprintf('- \t\t SUB_OP \n');
end
if(arr(i) == '*')
fprintf('* \t\t MULT_OP \n');
end
if(arr(i) == '/')
fprintf('/ \t\t DIV_OP \n');
end
if(arr(i) == '(')
fprintf('( \t\t LEFT_PAREN \n');
end
if(arr(i) == ')')
fprintf(') \t\t RIGHT_PAREN \n');
end
if(arr(i) == '=')
fprintf('= \t\t EQUAL_OP \n');
end
x = str2num(arr(i));
y = isletter(arr(i));
if(y || ~isempty(x))
strTwo = strcat(strTwo,arr(i));
end
if(~isspace(arr(i)) && ~isempty(strTwo))
if(~isempty(str2num(strTwo(1))) && any(regexp(strTwo,'.*[a-zA-]+.*')))
fprintf(strcat('Error. Potential variable (', strTwo, ') whose name starts with digit found \n'));
strTwo = '';
j = j + 1;
else
if isempty(str2num(strTwo(1)))
fprintf(strcat(strTwo, ('\t\t IDENTIFIER \n')));
strTwo = '';
j = j + 1;
end
end
end
end
end

matlab, user inputs a matrix into variable

I have this code for matlab to multiply matrixes how di i make the user add the matrixes and then use the matrixes in the code?
eg [n,m] = input(user inputs matrix here)
[n,m] = size(A);
[p,q] = size(B);
C = zeros(n,p);
if p~=m
error('Inner Matrix Dimensions Must Agree.')
end
for k = 1:n
for j = 1:q
temp=0;
for i = 1:p
temp = temp+(A(k,i)*B(i,j));
end
C(k,j) = temp;
end
end
You can use in the script:
A = input('input array A ');
B = input('input array B ');
[n,m] = size(A);
[p,q] = size(B);
C = zeros(n,p);
if p~=m
error('Inner Matrix Dimensions Must Agree.')
end
for k = 1:n
for j = 1:q
temp=0;
for i = 1:p
temp = temp+(A(k,i)*B(i,j));
end
C(k,j) = temp;
end
end
or you can write the above as a function:
function C = matrixmultiply(A,B)
[n,m] = size(A);
[p,q] = size(B);
C = zeros(n,p);
if p~=m
error('Inner Matrix Dimensions Must Agree.')
end
for k = 1:n
for j = 1:q
temp=0;
for i = 1:p
temp = temp+(A(k,i)*B(i,j));
end
C(k,j) = temp;
end
end
end

How to loop for desired output?

HI everyone I have a matrix input program as shown below. However I fail to loop for empty or complex or NaN input. I had tired various kind of method but is still not work. Sincerely hope to get advice from you all in order to solve this problem.
clear;clc
m=2;
for i = 1:m
for j = 1:m;
element_A = ['Enter the element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(element_A);
while isnan(A(i,j)) || ~isreal(A(i,j)) || isempty(A(i,j))
fprintf('Input not valid')
element_A = ['Enter the element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(element_A);
end
end
end
%% sample loop
m = str2double( input('??? : ', 's') );
while isnan(m) || ~isreal(m) || m<0
m = str2double( input('Enter valid value : ', 's') );
end
You should be checking for NaN, complex values and empty inputs before you assign them in A. You can do it like this:
m=2;
A = zeros(m); % You do not have to do this but it will increase the performance of your code.
for idx = 1:m
for jdx = 1:m;
element_A = ['Enter the element in row ' num2str(idx) ', col ' num2str(jdx) ': '];
inputElement = input(element_A);
while isempty(inputElement) || isnan(inputElement) || ~isreal(inputElement)
fprintf('Invalid input');
inputElement = input(element_A);
end
A(idx,jdx) = inputElement;
end
end
Notice that I moved isempty check to first place. || is a short circuit operator and will not check the next values is the first element gives true. If it is checked after, say isnan, it will give an error.

Vectorizing nested for loops in matlab

How do we vectorize this sort of code in matlab?
for i = 1:N
for k = 1:64
if (pixels(i,k)==1)
p(character(i),k)= p(character(i),k)+1;
end
end
end
The following should be equivalent:
[i,j] = find(pixels(i,k) == 1);
if ~isempty(i)
ind = sub2ind(size(p), character(i), j);
% or, equivalently:
% ind = character(i) + (j-1) * size(p,1);
p(ind) = p(ind) + 1;
end