Please help me to check my code

I have a problem when running my code. For some reason , my plotting doesn't work, thus my graph is complete blank when I run the code. Please kindly help me to check it. This is script using loop with quad
My function:
function [ g ] = myfun( w,u )
g=1./(9.*(w.^n)-8.*w);
end
My script:
u=[4:1:10];
for iu=1:length(u)
h=quad(@(w)myfunc(w,n(iu)),6,10);
v=h(:,1)
plot(u,v)
end
Thank in advance

Answers (1)

Try this:
myfunc = @(w,n) 1./(9.*(w.^n)-8.*w);
u=[4:1:10];
n = some vector;
for iu=1:length(u)
v(iu) = quad(@(w)myfunc(w,n(iu)),6,10);
end
figure(1)
plot(u, v)

4 Comments

Thank you so much. If it don't mind you, can you help me with this problem. I have the same problem when plotting. This is loop with ode45. I want to plot 'te' over 'u'
for iu = 1:length(w)
u1=u(iu)
[t,X,te,Xe,ie]=ode45(@(t,X)MyProb(t,X,u(iu)),tspan,x0,options);
tn=t1n(:,1)
end plot(u,te)
My pleasure.
I have no idea what you are doing with your differential equation.
I would reference it as a cell array (note the curly brackets ‘{}’ denoting cell array indexing):
[t{iu},X{u1},te{iu},Xe{iu},ie{iu}]=ode45(@(t,X)MyProb(t,X,u(iu)),tspan,x0,options);
Then in a separate loop, plot ‘X’ as a function of ‘t’ for each ‘u1’. Use the hold function. See the documentation on Cell Arrays (link) for information on working with them.
figure(2)
hold on
for iu = 1:length(w)
plot{t{iu}, X{iu})
end
hold off
grid
Note This is UNTESTED CODE. It should work.
Thank you so much. I greatly appreciate your help.
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 28 Aug 2017

Commented:

on 28 Aug 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!