I want to plot a simple function against time. The code is below. The issue is when I change the parameter c to be 1.3 the plot does not get drawn to the whole time span that I have specified, where as if I use c to be 3.4 it draws in the whole time span.
What is it that I am doing wrong that when c=1.3 the plot stops at x=1298 (I want it to be drawn all the way up to 2000)? Is it an issue with the function that I use to generate the values?
time=0:2000;
a=10^7;
b=1000;
c=log(2)/(3.4-2.1);
d=log(2)/((6.92)*24);
values=(a*b*exp((c-d).*time))./((a-b)+b*exp(c.*time));
figure
plot(time,values)
Check your values variable in the workspace.
You will see that from the index 1300, it gets the Inf or NaN value. These values cannot be plotted. I guess the denominator gets 0 at some point.
I run your code for 1.3
When you put 1.3 , then c get negative.
c =
-0.8664
Then all the values in final formula get a very close number to zero.
Related
I'm trying to plot and fill the area under the graph curve using area(). It works when I give the method simple functions, ie:
WS = linspace(0,100,500);
x = 2.*(WS)
area(WS,x)
but, for some reason, this method doesn't work in Octave with 'more complex functions'.
This is the script
WS = linspace(0,100,500);
TW_LCV = q.*( ( CD_min./WS) + k.*( (n./q).^2 ).*(WS) ); %the parameters are not relevant
figure()
plot(WS,TW_LCV, 'r');
hold on
area(WS, TW_LCV, 'FaceColor','y')
grid on;
I've tried the same script in Matlab, and it works. How can I fix this in Octave? The output:
Note. I'm using Windows 10
You are likely having infinite / nan values in your array (as in the example above, resulting from a division-by-zero operation).
Replace those values by suitable values (e.g. either get rid of inf values, or replace them by a suitably high value - or in your case possibly a zero value if that is deemed to be the limit of (x/WS)*WS for WS -> 0.
Once you only have appopriate numerical values, the area function will work as expected.
Having said that, if matlab does something with infinite values which octave treats differently, feel free to report it to the octave team in their bug tracker, since the octave team tends to treat deviations from matlab behaviour as 'bugs'.
I have this function below and I need to calculate the minimum and maximum of this function U, and also plotting the maximum and minimum value in 3D graph together with the function.
How can I write the code?
[x,y]=meshgrid(0:0.1:pi,0:0.1:2*pi);% x-theta,y-phi
a=90.7;b=36.2;c=12.9;
E=1.44;
U=-E.^2*(a.*sin(x).^2.*cos(y).^2+b.*sin(x).^2.*sin(y).^2+c.*cos(x).^2);
meshc(x,y,U)
xlabel('\theta')
ylabel('\Phi ')
zlabel('U')
I tired this way to find max but I don't know if i did it correct
max(U,[],1) %max row-wise
max(U,[],2) %max column-wise
and for the minimum it didn't work the same idea, also I didn't get the exact value of the maximum
As stated above, to simply find the maximum/minimum of your sampled function, use m = min(U(:)); M = max(U(:)). To be able to plot them, what you are missing are the coordinates that give you those values. For this, you will need the second output of the min/max functions, which gives you the index where the extreme happens.
A possible implementation (possibly not the best one) would be (might not work perfectly, I don't have matlab at hand):
[Ms,I] = max(U,[],1); %row-wise maximum and their indexes
[M,j] = max(Ms); %maximum among all rows and its index
Now i = I(j) is the location of the maximum. You can finally do plot3(x(i,j),y(i,j),U(i,j),'ro') to plot a big red circle in the maximums location, or whatever you like.
Note: I might have it backwards and it might be x(j,i), and so on. Just check. Of course you can do the same thing for min().
EDIT: I just remembered the ind2sub function , which solves all your problems. Following the syntax used above:
[M,ind] = max(U(:));
[i,j] = ind2sub(size(U),ind)
The rest holds the unchanged.
You can simply use something like
max(max(U))
this will find the maximum for your 2D matrix.
For the minimum you just have to replace max with min.
I working on a homework assignment and trying to solve the following problem:
Here is the code I have so far:
function [ ] = Activity45( Time )
%Homework 4
%Activity 4.5
t=Time;
A=[0:0.1:t];
B=3*exp(-(A/3)).*sin(pi.*A);
C=(B>0);
plot(A,B(C))
end
So I am trying to use a mask to extract the data from Matrix B in Matrix C. But I do not know how to match the data up between A and C, to then use plot().
Any help?
With plot(A(C), B(C)) you don't get the intended curve because you don't have values equal to zero. Instead the last two points to the left and right are connected with a line above 0. The right way would be to set the value on the Y-Axis to zero.
B(~C)=0;
plot(A,B);
For future formulas, it might be a good idea to use variable names matching the variable names in your formulas.
What is the difference between hist and imhist functions in Matlab? I have a matrix of color levels values loaded from image with imread and need to count entropy value of the image using histogram.
When using imhist the resulting matrix contains zeros in all places except the last one (lower-right) which contains some high value number (few thousands or so).
Because that output seems to be wrong, I have tried to use hist instead of imhist and the resulting values are much better, the matrix is fulfilled with correct-looking values instead of zeros.
However, according to the docs, imhist should be better in this case and hist should give weird results..
Unfortunately I am not good at Matlab, so I can not provide you with better problem description. I can add some other information in the future, though.
So I will try to better explain my problem..I have an image, for which I should count entropy and few other values (how much bytes it will take to save that image,..). I wrote this function and it works pretty well
function [entropy, bytes_image, bytes_coding] = entropy_single_pixels(im)
im = double(im);
histg = hist(im);
histg(histg==0) = [];
nzhist = histg ./ numel(im);
entropy = -sum(nzhist.*log2(nzhist));
bytes_image = (entropy*(numel(im))/8);
bytes_coding = 2*numel(unique(im));
fprintf('ENTROPY_VALUE:%s\n',num2str(entropy));
fprintf('BYTES_IMAGE:%s\n',num2str(bytes_image));
fprintf('BYTES_CODING:%s\n',num2str(bytes_coding));
end
Then I have to count the same, but I have to make "pairs" from pixels which are below each other. So I have only half the rows and the same count of columns. I need to express every unique pixel pair as a different number, so I multiplied the first one by 1000 and added the second one to it... Subsequently I need to actually apply the same function as in the first example, but that is the time, when I am getting weird numbers from the imhist function. When using hist, it seems to be OK, but I really don't think that behavior is correct, so that must be my error somewhere. I actually understand pretty good, to what I want to do, or at least I hope so, but unfortunately Matlab makes all that kind of hard for me :)
hist- compute histogram(count number of occurance of each pixel) in color image.........
imhist- compute histogram in two dimensional image.
Use im2double instead of double if you want to use imhist. The imhist function expects double or single-precision data to be in the [0,1] data range, which is why you see everything in the last bin of the histogram.
I'm having a problem sending a value from a GUI to an Embedded MATLAB Function (EMF) in a Simulink model. I get this value from a slider in my GUI and send it to an EMF block in my model. I can confirm that the value is being transferred correctly from my GUI to my Simulink block, since I can display the value with a display block in my model and see the value change when I change the slider position in my GUI. However I keep getting this error when I run my model:
Could not determine the size of this expression.
Function 'Kastl' (#18.282.283), line 14, column 1:
"f"
This is part of my EMF block code:
function y = input_par(u,fstart)
...
f_end = 1000;
f = fstart:f_end;
...
I believe MikeT is correct: you can't redefine the size of a variable in an embedded function. If you look at this Embedded MATLAB Function documentation page under the subsection Defining Local Variables, it says:
Once you define a variable, you cannot
redefine it to any other type or size
in the function body.
You will have to rework your embedded function such that the variables you declare are not changing size. Since I don't know what you are subsequently doing with the variable f, there's not much more specific help I can give you.
In general, if you absolutely need to use data that changes size, one solution is to pad the data with "garbage" values in order to maintain a constant size. For example:
MAX_ELEMS = 1000; % Define the maximum number of elements in the vector
f = [fstart:MAX_ELEMS nan(1,fstart-1)]; % Create vector and pad with NaNs
In the above example, the variable f will always have 1000 elements (assuming the value of fstart is an integer value less than or equal to 1000). The value NaN is used to pad the vector to the appropriate constant size. Any subsequent code would have to be able to recognize that a value of NaN should be ignored. Depending on what calculations are subsequently done in the embedded function, different pad values might be needed in place of NaN (such as 0, negative values, etc.).
I believe the issue you are running into is that you can't change a parameter during simulation that will cause the dimension of a signal to change. In your example, the code,
f = fstart:f_end;
changes size whenever fstart changes. I think this is what EMF block is complaining about. I don't have any easy workaround for this particular issue, but maybe there's an equivalent way of doing what you want that avoids this issue.