I'm trying to generate an action potential of the HH model, and I need to calculate the alphaN rates. However, at a few values, they are 0/0, so I need to use L'hopitals rule. I can't seem to make the script work. Can anyone please help?
How do I do an if statement for a range of numbers? Like:
if Vm == -50:-49
syms Vm;
x = diff((0.01.*(10-(Vm+60))));
y = diff((exp((10-(Vm+60))./10)-1));
alphaN = x./y;
else
alphaN = (0.01.*(10-(Vm+60)))./(exp((10-(Vm+60))./10)-1); % l/ms
betaN = 0.125*exp(-(Vm+60)/80); % 1/ms
end
plot(alphaN,Vm)
However, with the above script, I get matrix doesn't agree. How can I make this work? Hopefully it's just something I'm forgetting.
Thanks for the help!
To test whether Vm is between a and b, you write
if Vm >= a && Vm <= b %# include a and b
To test whether Vm is any integer between a and b
if any(Vm == a:b)
You can use a switch/case construct:
Vm_all = -50:50; %all Vm
for ii = 1:length(Vm)
Vm = Vm_all(ii);
switch Vm
case {-50,-49}
syms Vm;
x = diff((0.01.*(10-(Vm+60))));
y = diff((exp((10-(Vm+60))./10)-1));
alphaN(ii) = x./y;
betaN(ii) = NaN;
otherwise
alphaN(ii) = (0.01.*(10-(Vm+60)))./(exp((10-(Vm+60))./10)-1);
betaN(ii) = 0.125*exp(-(Vm+60)/80);
end
end
This way it doesn't matter if your particular values are in a row or whatever, just type them comma separated.
If you have an array with your exceptions like exc = [-50,-49,-12,42] you can use it as follow for switch/case:
case {exc(:)}
Related
I've got a set of data in an excel sheet that I've imported onto MATLAB however there are NaNs within that set of data. I've figured out some code in the main script to replace the NaN's into the wanted values:
max = x(:, 2);
min = x(:, 3);
for j = 1:length(max)
for k = 1:length(min)
if isnan (max(j))
max (j) = ((max(j-1)+max(j+1))/2);
elseif isnan (min(k))
min (k) = ((min(k-1)+min(k+1))/2);
end
end
end
However, I need to be able to turn this code into a user-defined function and call it from the main script instead of having all the calculations on the main script.
I've tried to start making the function:
function [missingmax, missingmin] = missing(max, min)
However, I could not figure the rest out.
function [max_x, min_x] = missing(x)
max_x = x(:, 2);
min_x = x(:, 3);
for jj = 1:length(max_x)
for kk = 1:length(min_x)
if isnan (max_x(jj))
max_x (jj) = ((max_x(jj-1)+max_x(jj+1))/2);
elseif isnan (min_x(kk))
min_x (kk) = ((min_x(kk-1)+min_x(kk+1))/2);
end
end
end
end
You were on the right track. Couple of things:
Your input is x, not min,max
Your outputs are min and max, not missingmax and missingmin
j denotes the imaginary unit It's not recommended for use as a variable, hence I changed it.
You called variables min and max. Don't do that. Ever. Seriously. Don't. If you manage to do min=4 and then try to calculate the minimum of an array, you'll get a bunch of errors. Basically: never use the name of a build-in function for a variable.
Since you do a linear interpolation, you don't need to define a function here. It already exists in Matlab : fillmissing
So you can replace missing values in x like that
x_filled = fillmissing(x,'linear')
I have a loop within a function that spits out values similar to:
E = 3,2,1,-1,-2
for
i = 1,2,3,4,5
I'm trying to extract the position where E becomes negative and then identify the step before it.
My attempt was something like
finalPos = find(i(E<0));
Firstly, it just doesn't seem right (my matlab syntax knowledge is poor as)
but secondly even if it did work it would tell me all positions where E is less than 0, where I only want to know the position before where E is no longer positive. i.e. E = 1, i = 3
Any help would be greatly appreciated!
Check the below:
E = [3,2,1,-1,-2] ;
idx = find(sign(E)==-1) % Get the sign and get index
idx = find(E<0) % Get by value
for i = 1:length(E)
if sign(E(i)) == -1
fprintf('E is Negative\n')
else
fprintf('E is Positive\n')
end
end
For last positive value
you can use a variable to store the last value
Example:-
E=[3,2,1,-1,-2]
finalpos=-1
for i = 1:5
if ( E<0)
finalpos=E(i-1);
break;
end
end
finalpos
I have a simple but interesting question. i tired hard to google it but my google got upset and giving me the same results...
i wanted to know is it possible to Update a constant variable form workspace command..
A Simple Example:
function y =StupidQuestion
a = 10; % some value
b =[5,6,7;1,2,8]; % some value
y = b*a % some operation
I forget to tell you that we can do it with simulink block by using below command
set_param('obj', 'parameter1', value1, 'parameter2', value2, ...)
i Want to use the assigned value for 3 weeks and without any reason i wants to change my values [a,b] to other but through command windows. any Idea. Waiting for your interesting Reply...................
You can set defaults for the inputs:
function y = foo(a,b)
if nargin < 1 || isempty(a), a = 10; end
if nargin < 2 || isempty(b), b = [5,6,7;1,2,8]; end
y = b*a
end
You can call foo() without inputs (and it will use the defaults for a and b) or supply your own values as: foo(12), foo(12,[10,20]), foo([],[23,23]), etc...
A possible way is to save some variables in an external file. Note that in this case a and b are only in the function workspace (you won't see their values unless you load the contents of test.mat separately). I'm passing the filename in rather than hard-coding it in case you need to switch between multiple settings.
Personally I would prefer to have a human-readable data file, but the concept remains the same (you'd just need some parser function which returned values for a and b given a file).
a = 10; % some value
b =[5,6,7;1,2,8]; % some value
save('test.mat','a','b');
clear a b;
function y = savedvariables(filename)
load(filename);
y = b*a; % some operation
end
y = savedvariables('test.mat');
I'm trying to code a function calculating difference quotients. I need to this for polynomial interpolation. Given nodes x = linspace(a,b,n+1), function values at the nodes y = func(x) I want to find the values of difference quotients f[x0], f[x_0,x_1], ..., f[x_0,x_1,...,x_n]. To calculate f[x_0,x_1,...,x_n] I will need f[x_0,x_1,...,x_(n-1)], etc., thus it would be a good idea save the intermediate steps on my way to f[x_0,x_1,...,x_n], so that on my way to f[x_0,x_1,...,x_n] I will save the preceding difference quotients as elements of a vector.
Could someone tell me how to correct my code in order to save appropriate difference quotient values? Here's the code:
function [fx, all_fx] = ilo2(a,b,x,y,fx,all_fx)
if a == b
fx(end+1) = y(a);
if a == 1
all_fx(end+1) = fx(end);
end
return
end
a;
b;
[c, all_fx] = ilo2(a+1,b,x,y,fx,all_fx);
[d, all_fx] = ilo2(a,b-1,x,y,fx,all_fx);
fx(end+1) = (c-d)/(b-a);
if a == 1
all_fx(end+1) = fx(end);
end
end
The difference quotients I need are under 'if a == 1' condition.
Ok, I think I fixed it:
function [all_fx,fx] = ilo(a,b,x,y,all_fx,fx)
if a == b
fx(end+1) = y(a);
if a == 1
all_fx(end+1) = fx(end);
end
return
end
[all_fx,c] = ilo(a+1,b,x,y,all_fx,fx);
[all_fx,d] = ilo(a,b-1,x,y,all_fx,fx);
fx(end+1) = (c-d)/(b-a);
if a == 1
all_fx(end+1) = fx(end);
end
end
So I have one problem with my code, which is that one variable is not working as it's supposed to do. Here are the codes I'm using:
format long
f = inline('-x.^2');
for i = 0:10
[I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i);
end
for i = 0:10
trunk(i+1) = I(i+1) - log(2);
end
hold on
grid on
plot(log(h),log(trunk),'r+')
t = -7:0;
c = polyfit(log(h),log(trunk),1);
yy = polyval(c,t);
plot(t,yy)
grid off
hold off
koefficienter = real(c)
and also this:
function [ I,h,tid ] = trapets(f,a,b,n )
h=(b-a)/n;
tic;
I=(f(a)+f(b));
for k=2:2:n-2
I = I+2*f(a+k*h);
end
for k = 1:2:n-1
I = I + 4*f(a+k*h);
end
I = I * h/3;
tid = toc;
end
So the problem here is that the variable I is not changing value. It gets 11 values when I run the first code (I don't run the last code I wrote, it's only used by the first one), but the values are all the same. I don't know if the problem is that the variable n, which I use in the the second code, never change value, although I'm trying to do that with the "2^i" part in "trapets(f,0,1,2^i)". If the case is that n never change value, is there a solution to do that?
And if the problem is not the variable n, why doesn't the variable I change value in the code?
After running your program I found out the I always equals -1/h after the for loops, which makes I = I * h/3; always give you the same result.