Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix multiplication woes

Subject: Matrix multiplication woes

From: Hugh

Date: 5 Nov, 2010 16:54:04

Message: 1 of 8

[code]

function U= sabrCall(S,K,sig,r,T,nu,beta,rho,Smax,dS,Numax,dnu,dt,greek)

if nargin < 14, greek =1;end

NS=round(1/dS);
dS=1/NS;
Nnu=round(1/dnu);
dnu=1/Nnu;
Nt=round(T/dt);
dt=T/Nt;

Layers=zeros(2,NS+1,Nnu+1);

tpast=1;
tnow=2;

Svalue=0:dS:Smax;
Vvalue=0:dnu:Numax;

payoff=max(Svalue-K,0);
Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);

[S,VOL]= meshgrid(0:dS:Smax,0:dnu:Numax);

Layers(tpast,1:NS,1:Nnu)=max(vetS-K,0);

U =1;
end

[/code]

The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);

is coming back as a dimensions mismatch but I don't see why? Moreover when I try to run the program with stops nothing shows in the workspace? Why is this?? It makes it alot harder to debug.

Subject: Matrix multiplication woes

From: Sean

Date: 5 Nov, 2010 17:13:04

Message: 2 of 8


>
> The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
>

Put a break point at this line and save. Then call the function, if the debugger does not stop at this line, you have a file path issue. If it does (which it should we hope), type:
whos
at the command line and it'll tell you the sizes. If it's not obvious then, report the error message and all of the sizes back here.

Subject: Matrix multiplication woes

From: Hugh

Date: 5 Nov, 2010 17:25:05

Message: 3 of 8

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ib1dv0$jns$1@fred.mathworks.com>...
>
> >
> > The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
> >
>
> Put a break point at this line and save. Then call the function, if the debugger does not stop at this line, you have a file path issue. If it does (which it should we hope), type:
> whos
> at the command line and it'll tell you the sizes. If it's not obvious then, report the error message and all of the sizes back here.

tnx Sean.

I changed the name of the finction then changed the name of the file to match using saveAs would this give me the file path issue you mention?

Also to be clear in the problem

> > The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);

the rhs works when I assign it to a new veriable say A, however I can not get it to assigmto Layers but I think it should. Will become clearer I suppose when this filepath issue is resolved.

Subject: Matrix multiplication woes

From: Jan Simon

Date: 5 Nov, 2010 17:27:04

Message: 4 of 8

Dear Hugh,

> Layers=zeros(2,NS+1,Nnu+1);
> Svalue=0:dS:Smax;
> payoff=max(Svalue-K,0);
> Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);

Now the first dimension of "payoff'*ones(1,Nnu+1)" does not match the second dimension of "Layers(tpast,:,:)".
You can simply inspect this using the debugger:
  dbstop if error
Then run the program until it stops. Type in the CommandWindow:
  size(Layers)
  size(payoff'*ones(1,Nnu+1))

The debugger is very useful. It is really recommended to learn to work with it.

Kind regards, Jan

Subject: Matrix multiplication woes

From: Sean

Date: 5 Nov, 2010 17:35:04

Message: 5 of 8

"Hugh " <h_a_patience@hotmail.com> wrote in message <ib1elh$5nk$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ib1dv0$jns$1@fred.mathworks.com>...
> >
> > >
> > > The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
> > >
> >
> > Put a break point at this line and save. Then call the function, if the debugger does not stop at this line, you have a file path issue. If it does (which it should we hope), type:
> > whos
> > at the command line and it'll tell you the sizes. If it's not obvious then, report the error message and all of the sizes back here.
>
> tnx Sean.
>
> I changed the name of the finction then changed the name of the file to match using saveAs would this give me the file path issue you mention?
>
> Also to be clear in the problem
>
> > > The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
>
> the rhs works when I assign it to a new veriable say A, however I can not get it to assigmto Layers but I think it should. Will become clearer I suppose when this filepath issue is resolved.

I don't know about the file path issue. You can type
which
at the command line to see where it thinks it is.

As for the RHS working when not assigning this makes perfect sense actually.
You are trying to assign a 2-d matrix (has to be scalar, vector or full 2d since this is all mtimes works for) to a 3d slice.

You'll need to PERMUTE or RESHAPE it to get it to fit. Something like:
reshape(rhs,[1 size(rhs,1),size(rhs,2)])

Subject: Matrix multiplication woes

From: Hugh

Date: 5 Nov, 2010 17:40:08

Message: 6 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ib1ep8$dhi$1@fred.mathworks.com>...
> Dear Hugh,
>
> > Layers=zeros(2,NS+1,Nnu+1);
> > Svalue=0:dS:Smax;
> > payoff=max(Svalue-K,0);
> > Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
>
> Now the first dimension of "payoff'*ones(1,Nnu+1)" does not match the second dimension of "Layers(tpast,:,:)".
> You can simply inspect this using the debugger:
> dbstop if error
> Then run the program until it stops. Type in the CommandWindow:
> size(Layers)
> size(payoff'*ones(1,Nnu+1))
>
> The debugger is very useful. It is really recommended to learn to work with it.
>
> Kind regards, Jan

OK debugger started working without me really doing anything ( am aware of).

Previously in the workspace I could only access other functions I went to run once more and subbdenly SABR is there?

Its nice but I'd like to know what happened for future reference

Subject: Matrix multiplication woes

From: Roger Stafford

Date: 5 Nov, 2010 18:32:03

Message: 7 of 8

"Hugh " <h_a_patience@hotmail.com> wrote in message <ib1crc$6ep$1@fred.mathworks.com>...
> [code]
>
> function U= sabrCall(S,K,sig,r,T,nu,beta,rho,Smax,dS,Numax,dnu,dt,greek)
>
> if nargin < 14, greek =1;end
>
> NS=round(1/dS);
> dS=1/NS;
> Nnu=round(1/dnu);
> dnu=1/Nnu;
> Nt=round(T/dt);
> dt=T/Nt;
>
> Layers=zeros(2,NS+1,Nnu+1);
>
> tpast=1;
> tnow=2;
>
> Svalue=0:dS:Smax;
> Vvalue=0:dnu:Numax;
>
> payoff=max(Svalue-K,0);
> Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
>
> [S,VOL]= meshgrid(0:dS:Smax,0:dnu:Numax);
>
> Layers(tpast,1:NS,1:Nnu)=max(vetS-K,0);
>
> U =1;
> end
>
> [/code]
>
> The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
>
> is coming back as a dimensions mismatch but I don't see why? Moreover when I try to run the program with stops nothing shows in the workspace? Why is this?? It makes it alot harder to debug.
- - - - - - - - - -
  In the assignment that is giving you errors

 Layers(tpast,:,:)=payoff'*ones(1,Nnu+1)

the left side is 1 by NS+1 by Nnu+1 in size while the right side is n by Nnu+1 where n is the number of elements in 0:dS:Smax. If Smax is other than 1, then clearly n will differ from NS+1. However because of round off error, you might find cases where there is a difference even when Smax is exactly 1. This latter is because the assignment dS = 1/NS cannot be calculated exactly in general.

Roger Stafford

Subject: Matrix multiplication woes

From: Hugh

Date: 8 Nov, 2010 18:32:08

Message: 8 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ib1ij3$phk$1@fred.mathworks.com>...
> "Hugh " <h_a_patience@hotmail.com> wrote in message <ib1crc$6ep$1@fred.mathworks.com>...
> > [code]
> >
> > function U= sabrCall(S,K,sig,r,T,nu,beta,rho,Smax,dS,Numax,dnu,dt,greek)
> >
> > if nargin < 14, greek =1;end
> >
> > NS=round(1/dS);
> > dS=1/NS;
> > Nnu=round(1/dnu);
> > dnu=1/Nnu;
> > Nt=round(T/dt);
> > dt=T/Nt;
> >
> > Layers=zeros(2,NS+1,Nnu+1);
> >
> > tpast=1;
> > tnow=2;
> >
> > Svalue=0:dS:Smax;
> > Vvalue=0:dnu:Numax;
> >
> > payoff=max(Svalue-K,0);
> > Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
> >
> > [S,VOL]= meshgrid(0:dS:Smax,0:dnu:Numax);
> >
> > Layers(tpast,1:NS,1:Nnu)=max(vetS-K,0);
> >
> > U =1;
> > end
> >
> > [/code]
> >
> > The line Layers(tpast,:,:)=payoff'*ones(1,Nnu+1);
> >
> > is coming back as a dimensions mismatch but I don't see why? Moreover when I try to run the program with stops nothing shows in the workspace? Why is this?? It makes it alot harder to debug.
> - - - - - - - - - -
> In the assignment that is giving you errors
>
> Layers(tpast,:,:)=payoff'*ones(1,Nnu+1)
>
> the left side is 1 by NS+1 by Nnu+1 in size while the right side is n by Nnu+1 where n is the number of elements in 0:dS:Smax. If Smax is other than 1, then clearly n will differ from NS+1. However because of round off error, you might find cases where there is a difference even when Smax is exactly 1. This latter is because the assignment dS = 1/NS cannot be calculated exactly in general.
>
> Roger Stafford

Ahhhh yes!!

Thank you

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us